<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
	xmlns:xhtml="http://www.w3.org/1999/xhtml"
	xmlns="http://www.w3.org/1999/xhtml" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns:msxsl="urn:schemas-microsoft-com:xslt"
	xmlns:ram="urn:un:unece:uncefact:data:draft:ReusableAggregateBusinessInformationEntity:1"
    xmlns:rsm="urn:un:unece:uncefact:data:draft:Test:1" 
	xmlns:ebforms="http://code.google.com/p/eb-forms"
	xmlns:sh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"
	xmlns:eanucc="urn:ean.ucc:2" 
	xmlns:order="urn:ean.ucc:order:2" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:2" 
	xmlns:qdt="urn:un:unece:uncefact:data:standard:QualifiedDataType:2" 
	xmlns:ebram="urn:un:unece:uncefact:data:draft:ReusableAggregateBusinessInformationEntity:1"
	xmlns:ebrsm="urn:un:unece:uncefact:data:draft:Test:1" 
	xmlns:ebsh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader" 
	xmlns:ebeanucc="urn:ean.ucc:2" 
	xmlns:eborder="urn:ean.ucc:order:2" 
	xmlns:xf="http://www.w3.org/2002/xforms"
	xmlns:ccts="urn:un:unece:uncefact:documentation:standard:CoreComponentsTechnicalSpecification:2"
	xmlns:ev="http://www.w3.org/2001/xml-events"
	version="1.0" exclude-result-prefixes="ebforms">

	<!--	xmlns:msxsl="urn:schemas-microsoft-com:xslt" -->
	<!-- xmlns:msxsl="http://icl.com/saxon" -->

<!-- context is instance (template or received message) -->
 
<xsl:variable name="global.annotations.node"  select="document('annotations.xml')" />

<xsl:variable name="inboxdir" select="$global.annotations.node/ebforms:annotations/ebforms:directories/ebforms:directory[@name='inbox']/@location" />
<xsl:variable name="templatesdir" select="$global.annotations.node/ebforms:annotations/ebforms:directories/ebforms:directory[@name='templates']/@location" />
<xsl:variable name="outboxdir" select="$global.annotations.node/ebforms:annotations/ebforms:directories/ebforms:directory[@name='outbox']/@location" />
<xsl:variable name="schemadir" select="$global.annotations.node/ebforms:annotations/ebforms:directories/ebforms:directory[@name='schemas']/@location" /> 
<xsl:variable name="tempdir" select="'../Program/'" /> 
<xsl:variable name="annotationsdir" select="'../Program/'" /> 

<xsl:variable name="global.template.instance.node" select="/" />
<xsl:variable name="global.template.instance.name" select="local-name( * )" /> 
<xsl:variable name="global.template.instance.ns" select="namespace-uri( * )" />
 <xsl:variable name="global.template.instance.prefix">
	<xsl:choose>
		<xsl:when test="contains(name( * ), ':')">
			<xsl:value-of select="substring-before(name( * ),':')" />
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="''" />
		</xsl:otherwise>
	</xsl:choose>
</xsl:variable>
 
<xsl:variable name="full.instance.name">
	<xsl:choose>
		<xsl:when test="$global.template.instance.prefix!=''"> 
			<xsl:value-of select="concat($global.template.instance.prefix,':',$global.template.instance.name)" />
		</xsl:when>
		<xsl:otherwise>	
			<xsl:value-of select="$global.template.instance.name" />
		</xsl:otherwise>
	</xsl:choose> 
</xsl:variable> 

<xsl:variable name="instance.id" select="concat($global.template.instance.name,'-1')" />
<xsl:variable name="model.id" select="$global.template.instance.name" />

<!-- <xsl:variable name="global.instance" select="." /> -->

<xsl:variable name="global.doc.schema.name">
		<xsl:choose>
			<xsl:when test="substring-after(//@*[contains(name(),'schemaLocation')],' ')!=''">
				<xsl:value-of select="$schemadir" />
				<xsl:call-template name="get.file.from.path">
					<xsl:with-param name="filepath" select="substring-after(//@*[contains(name(),'schemaLocation')],' ')" /> 
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="$global.annotations.node/ebforms:annotations/ebforms:core-component[@tag=$global.template.instance.name]/ebforms:element[@class='document']/ebforms:schema/@value!=''">
					<xsl:value-of select="concat($schemadir,$global.annotations.node/ebforms:annotations/ebforms:core-component[@tag=$global.template.instance.name]/ebforms:element[@class='document']/ebforms:schema/@value)"/>  
			</xsl:when> 
			<xsl:otherwise>
				<xsl:value-of select="concat($schemadir,'Default.xsd')" />
			</xsl:otherwise>
		</xsl:choose>
</xsl:variable> 

<xsl:variable name="global.doc.schema.node" select="document($global.doc.schema.name)" />

<!-- take from invokation. XForms processor rather than browser -->
<xsl:variable name="global.xfprocessor" select="$global.annotations.node/ebforms:annotations/ebforms:environment/ebforms:XFprocessor/@name" /> 
<!-- <xsl:variable name="global.xfprocessor" select="'IE'" /> -->
        
<xsl:variable name="quot">'</xsl:variable>
<xsl:variable name="doublequote">"</xsl:variable>
<xsl:variable name="comma">,</xsl:variable>

<!-- Look up the den of the instance root, if not annotated, reconstruct from tag -->
<xsl:variable name="global.instance.den">
	<xsl:choose>
		<xsl:when test="$global.doc.schema.node/xsd:schema/xsd:element[@name=$global.template.instance.name]/xsd:annotation/xsd:documentation/ccts:DictionaryEntryName!=''">
			<xsl:value-of select="$global.doc.schema.node/xsd:schema/xsd:element[@name=$global.template.instance.name]/xsd:annotation/xsd:documentation/ccts:DictionaryEntryName" />
		</xsl:when>
		<xsl:otherwise>
			<xsl:call-template name="CamelCase2Words">
				<xsl:with-param name="input" select="$global.template.instance.name" />
				<xsl:with-param name="sep" select="'_ '" />
			</xsl:call-template>
			<xsl:value-of select="'. Details'" />
		</xsl:otherwise>
	</xsl:choose>
</xsl:variable>

<!-- determine the mode. Read from annotation file. Later take from invokation -->
<xsl:variable name="mode">
	<xsl:choose>
		<xsl:when test="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$global.instance.den]/ebforms:element[@class='document']/ebforms:mode/@value!=''">
			<xsl:value-of select="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$global.instance.den]/ebforms:element[@class='document']/ebforms:mode/@value" />
		</xsl:when>
		<xsl:otherwise> 
			<xsl:value-of select="'new'" />
		</xsl:otherwise>
	</xsl:choose> 
</xsl:variable>

<!--	The target namespace of the message we are transforming. The instance created should remain
		in this namespace. Probably obsolete -->
<!-- <xsl:variable name="global.ns.target" select="$global.doc.schema.node/xsd:schema/@targetNamespace" /> -->
<!-- <xsl:variable name="global.schema.root.prefix">rsm</xsl:variable> -->

<!-- collect all relevant attributes from instance, schema and annotations to assemble the XForms document -->  
<xsl:variable name="global.element.types.used">
	<xsl:call-template name="write.instance.elements" > 
		<xsl:with-param name="file" select="concat($tempdir,'input.xml')"/>
		<xsl:with-param name="level" select="'1'"/>
		<xsl:with-param name="parent-den" select="$global.instance.den" />
	</xsl:call-template> 
</xsl:variable>
 
 <xsl:template name="write.instance.elements">
	<xsl:param name="file"/>
	<xsl:param name="level"/>
	<xsl:param name="parent-den" />

	<xsl:variable name="instance.name" >
		<xsl:value-of select="local-name(*)" />
	</xsl:variable>		 
		
	<xsl:variable name="instance.schema.name">
			<xsl:choose>
				<xsl:when test="substring-after(//@*[contains(name(),'schemaLocation')],' ')!=''">
					<xsl:value-of select="$schemadir"/>
					<xsl:call-template name="get.file.from.path">
						<xsl:with-param name="filepath" select="substring-after(//@*[contains(name(),'schemaLocation')],' ')" />
					</xsl:call-template> 
				</xsl:when> 
				<xsl:when test="$global.annotations.node/ebforms:annotations/ebforms:core-component[@tag=$instance.name]/ebforms:element[@class='document' and ebforms:parent/@value='$parent-den']/ebforms:schema/@value!=''">
					<xsl:value-of select="concat($schemadir,$global.annotations.node/ebforms:annotations/ebforms:core-component[@tag=$instance.name]/ebforms:element[@class='document' and ebforms:parent/@value='$parent-den']/ebforms:schema/@value)"/>  
				</xsl:when> 
				<xsl:when test="$global.annotations.node/ebforms:annotations/ebforms:core-component[@tag=$instance.name]/ebforms:element[@class='document']/ebforms:schema/@value!=''">
					<xsl:value-of select="concat($schemadir,$global.annotations.node/ebforms:annotations/ebforms:core-component[@tag=$instance.name]/ebforms:element[@class='document']/ebforms:schema/@value)"/>  
				</xsl:when> 
				<xsl:otherwise>
					<xsl:value-of select="concat($schemadir,'Default.xsd')" />
				</xsl:otherwise> 
			</xsl:choose> 
	</xsl:variable> 
		
	<xsl:variable name="instance.den">
		<xsl:choose>
			<xsl:when test="document($instance.schema.name)/xsd:schema/xsd:element[@name=$instance.name]/xsd:annotation/xsd:documentation/ccts:DictionaryEntryName!=''"> 
				<xsl:value-of select="document($instance.schema.name)/xsd:schema/xsd:element[@name=$instance.name]/xsd:annotation/xsd:documentation/ccts:DictionaryEntryName" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="CamelCase2Words">
					<xsl:with-param name="input" select="$instance.name" />
					<xsl:with-param name="sep" select="'_ '" />
				</xsl:call-template> 
				<xsl:value-of select="'. Details'" />
			</xsl:otherwise> 
		</xsl:choose>
	</xsl:variable> 

	<xsl:variable name="instance.mode" >
		<xsl:choose>
			<xsl:when test="$level=1">
				<xsl:value-of select="$mode" />
			</xsl:when>
			<xsl:when test="$level=2"> 
				<xsl:choose>
					<xsl:when test="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$instance.den]/ebforms:element[@class='document' and ebforms:parent/@value=$parent-den]/ebforms:mode/@value!=''">
						<xsl:value-of select="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$instance.den]/ebforms:element[@class='document' and ebforms:parent/@value=$parent-den]/ebforms:mode/@value" />
					</xsl:when>
					<xsl:when test="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$instance.den]/ebforms:element[@class='document']/ebforms:mode/@value!=''">
						<xsl:value-of select="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$instance.den]/ebforms:element[@class='document']/ebforms:mode/@value" />
					</xsl:when> 
					<xsl:otherwise> 
						<xsl:value-of select="'new'" /> 
					</xsl:otherwise>
				</xsl:choose> 
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="'read'" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>		 

	<xsl:element name="ebforms:instance">

	<xsl:attribute name="name">
		<xsl:value-of select="$instance.name" />
	</xsl:attribute> 
	<xsl:attribute name="file">
		<xsl:value-of select="$file" />
	</xsl:attribute> 
	<xsl:attribute name="level">
		<xsl:value-of select="$level" />
	</xsl:attribute> 
	<xsl:attribute name="mode">
		<xsl:value-of select="$instance.mode" />
	</xsl:attribute> 

  	<xsl:element name="ebforms:full-name">
		<xsl:value-of select="name(*)" />
	</xsl:element> 
	<xsl:element name="ebforms:prefix">
		<xsl:value-of select="substring-before(name(*),':')" />
	</xsl:element> 
	<xsl:element name="ebforms:namespace">
		<xsl:value-of select="namespace-uri( * )" />
	</xsl:element>
	<xsl:element name="ebforms:schema">
			<xsl:attribute name="show">
				<xsl:choose>
					<xsl:when test="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$instance.den]/ebforms:element[@class='document' and ebforms:parent/@value='$parent-den']/ebforms:schema/@show!=''">
						<xsl:value-of select="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$instance.den]/ebforms:element[@class='document' and ebforms:parent/@value='$parent-den']/ebforms:schema/@show" />
					</xsl:when>
					<xsl:when test="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$instance.den]/ebforms:element[@class='document']/ebforms:schema/@show!=''">
						<xsl:value-of select="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$instance.den]/ebforms:element[@class='document']/ebforms:schema/@show" />
					</xsl:when>
					<xsl:otherwise> 
						<xsl:value-of select="'true'" />
					</xsl:otherwise>
				</xsl:choose> 
			</xsl:attribute>
			<xsl:value-of select="$instance.schema.name" />
</xsl:element> 

	<xsl:element name="ebforms:den">
		<xsl:value-of select="$instance.den" />
	</xsl:element> 
	
<!--	<xsl:element name="ebforms:strippedDen">
		<xsl:call-template name="GetStrippedDen">
			<xsl:with-param name="den" select="$instance.den" />
		</xsl:call-template>
	</xsl:element> -->
	
	 <xsl:element name="ebforms:width">
		<xsl:call-template name="FindPropertyValue">
			<xsl:with-param name="den.current" select="$instance.den"/>
			<xsl:with-param name="den.start" select="$instance.den"/>
			<xsl:with-param name="property" select="'width'" />
			<xsl:with-param name="class" select="'core-component'"/>
			<xsl:with-param name="value" select="'value'"/>
			<xsl:with-param name="mode" select="$instance.mode"/>
		</xsl:call-template>
	</xsl:element>
	<xsl:element name="ebforms:class">
		<xsl:call-template name="FindPropertyValue">
			<xsl:with-param name="den.current" select="$instance.den"/>
			<xsl:with-param name="den.start" select="$instance.den"/>
			<xsl:with-param name="property" select="'class'" />
			<xsl:with-param name="class" select="'core-component'"/>
			<xsl:with-param name="value" select="'value'"/>
			<xsl:with-param name="mode" select="$instance.mode"/>
		</xsl:call-template>
	</xsl:element>
	
	<xsl:element name="ebforms:businessTerm">
 		<xsl:call-template name="getBusinessTerm">
			<xsl:with-param name="den" select="$instance.den" />
			<xsl:with-param name="annotated.business.term" select="document($instance.schema.name)/xsd:schema/xsd:element[@name=$instance.name]/xsd:annotation/xsd:documentation/ccts:BusinessTerm" />
		</xsl:call-template> 
	</xsl:element>
	 
	<xsl:element name="ebforms:caption-width">
		<xsl:call-template name="FindPropertyValue">
			<xsl:with-param name="den.current" select="$instance.den"/>
			<xsl:with-param name="den.start" select="$instance.den"/>
			<xsl:with-param name="property" select="'width'" />
			<xsl:with-param name="class" select="'caption'"/>
			<xsl:with-param name="value" select="'value'"/>
			<xsl:with-param name="mode" select="$instance.mode"/>
		</xsl:call-template>
	</xsl:element>
	<xsl:element name="ebforms:caption-class">
		<xsl:call-template name="FindPropertyValue">
			<xsl:with-param name="den.current" select="$instance.den"/>
			<xsl:with-param name="den.start" select="$instance.den"/>
			<xsl:with-param name="property" select="'class'" />
			<xsl:with-param name="class" select="'caption'"/>
			<xsl:with-param name="value" select="'value'"/>
			<xsl:with-param name="mode" select="$instance.mode"/>
		</xsl:call-template>
	</xsl:element>

	<xsl:variable name="instance.show">
		<xsl:choose>
			<xsl:when test="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$instance.den and @mode=$instance.mode and ebforms:element[@class='document' and ebforms:parent/@value=$parent-den]]/ebforms:element[@class='core-component']/ebforms:show/@value!=''">
				<xsl:value-of select="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$instance.den and @mode=$instance.mode and ebforms:element[@class='document' and ebforms:parent/@value=$parent-den]]/ebforms:element[@class='core-component']/ebforms:show/@value" />
			</xsl:when>
			<xsl:when test="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$instance.den and ebforms:element[@class='document' and ebforms:parent/@value=$parent-den]]/ebforms:element[@class='core-component']/ebforms:show/@value!=''">
				<xsl:value-of select="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$instance.den and ebforms:element[@class='document' and ebforms:parent/@value=$parent-den]]/ebforms:element[@class='core-component']/ebforms:show/@value" />
			</xsl:when>
			<xsl:when test="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$instance.den and @mode=$instance.mode]/ebforms:element[@class='core-component']/ebforms:show/@value!=''">
				<xsl:value-of select="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$instance.den and @mode=$instance.mode]/ebforms:element[@class='core-component']/ebforms:show/@value" />
			</xsl:when>
			<xsl:when test="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$instance.den]/ebforms:element[@class='core-component']/ebforms:show/@value!=''">
				<xsl:value-of select="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$instance.den]/ebforms:element[@class='core-component']/ebforms:show/@value" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="'true'" />
			</xsl:otherwise>
		</xsl:choose> 
	</xsl:variable> 
	 	
	 <xsl:element name="ebforms:show">
		 <xsl:value-of select="$instance.show" />
	 </xsl:element> 

	<xsl:variable name="instance.type" select="document($instance.schema.name)/xsd:schema/xsd:element[@name=$instance.name]/@type" /> 
  
    <xsl:for-each select="./*/*[$level!='3' or name(.)!=name(preceding-sibling::*[1])]"> 
	   <xsl:call-template name="GetElementType">
			<xsl:with-param name="parent.type" select="$instance.type" />
			<xsl:with-param name="parent.schema.name"  select="$instance.schema.name"/>
			<xsl:with-param name="parent.schema.node"  select="document($instance.schema.name)/xsd:schema/xsd:complexType[@name=$instance.type]"/>
			<xsl:with-param name="parent.instance.node"  select="."/>
			<xsl:with-param name="parent.name"  select="$instance.name"/>
			<xsl:with-param name="parent.derivation.path"  select="''"/>
			<xsl:with-param name="level"  select="$level"/>
			<xsl:with-param name="dercount"  select="'1'"/>
			<xsl:with-param name="parent.mode"  select="$instance.mode"/>
			<xsl:with-param name="parent.show"  select="$instance.show"/> 
			
		</xsl:call-template> 
    </xsl:for-each> 
     
	<xsl:if test="$level='1' or $level='2' "> 
		<xsl:for-each select="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$instance.den]/ebforms:element[@class='document']/ebforms:refer-document/@value">
			<xsl:if test=".!=''">
				<xsl:variable name="instance.file" select="concat($inboxdir,.)" />
				<xsl:for-each select="document($instance.file)">
					<xsl:call-template name="write.instance.elements">
						<xsl:with-param name="file" select="$instance.file"/>
						<xsl:with-param name="level" select="'3'"/>
						<xsl:with-param name="parent-den" select="$instance.den" />
					</xsl:call-template>
				</xsl:for-each> 
			</xsl:if> 
		</xsl:for-each>
	</xsl:if>	

	<xsl:if test="$level='1' "> 
		<xsl:for-each select="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=$instance.den]/ebforms:element[@class='document']/ebforms:reply-document/@value">
			<xsl:if test=".!=''">
				<xsl:variable name="instance.file" select="concat($templatesdir,.)" />
				<xsl:for-each select="document($instance.file)">
					<xsl:call-template name="write.instance.elements">
						<xsl:with-param name="file" select="$instance.file"/>
						<xsl:with-param name="level" select="'2'"/>
						<xsl:with-param name="parent-den" select="$instance.den" />
					</xsl:call-template>
				</xsl:for-each>
			</xsl:if>
		</xsl:for-each>
	</xsl:if>	
</xsl:element>
</xsl:template> 
  
<!-- output parameters HTML form -->
<!--<xsl:if test="$global.xfprocessor='FP' "> -->
	<xsl:output media-type="application/xml" method="xml" encoding="UTF-8" version="1.0" indent="yes" omit-xml-declaration="yes"/>
<!-- </xsl:if>
<xsl:if test="$global.xfprocessor='XSLTForms' ">
	<xsl:output media-type="application/xml" method="xml" encoding="iso-8859-1" version="1.0" indent="yes" omit-xml-declaration="no"/>
</xsl:if> -->


<!-- Start at the root of the message or template -->
<xsl:template match="/">
     <xsl:call-template name="GenerateForm" />
</xsl:template>

<!-- context is instance root -->
<xsl:template name="GenerateForm">
    
    <!-- set the name of the HTML file. Probably not needed, defined by XSLT engine call -->
	<!-- <xsl:variable name="output" select="'output.html'" /> -->

	<html xsl:exclude-result-prefixes="sh eanucc order rsm ram">
      <!-- copy namespaces from annotations -->
		<xsl:copy-of select="$global.annotations.node/ebforms:annotations/namespace::ebram" />
		<xsl:copy-of select="$global.annotations.node/ebforms:annotations/namespace::ebrsm" /> 
		<xsl:copy-of select="$global.annotations.node/ebforms:annotations/namespace::ebsh" />
		<xsl:copy-of select="$global.annotations.node/ebforms:annotations/namespace::ebeanucc" /> 
		<xsl:copy-of select="$global.annotations.node/ebforms:annotations/namespace::eborder" />
		<xsl:copy-of select="$global.annotations.node/ebforms:annotations/namespace::ebforms" /> 
		
		<xsl:copy-of select="$global.annotations.node/ebforms:annotations/@*"/>

      <head>
        <title>
          <xsl:value-of  select="$global.template.instance.name" />
        </title>
     
    <!-- for debugging: -->                  
  <!-- kkk<xsl:copy-of select="$global.element.types.used" />mmm   -->
	<!-- in case the browser is IE write the Formsplayer intro -->
        <xsl:if test="$global.xfprocessor='FP' ">
          <object id="FormsPlayer" width="0" height="0" 
            classid="CLSID:4D0ABA11-C5F0-4478-991A-375C4B648F58">
            <b>FormsPlayer has failed to load! Please check your installation.</b>
          </object>
          <xsl:processing-instruction name="import">namespace="xf" implementation="#FormsPlayer"</xsl:processing-instruction>
        </xsl:if> 

		<!-- link to stylesheet  -->
		<xsl:if test="$global.xfprocessor='FP' ">
			<xsl:element name="link">
				<xsl:attribute name="rel">
					<xsl:value-of select="'stylesheet'"/>
				</xsl:attribute>
				<xsl:attribute name="type">
					<xsl:value-of select="'text/css'"/>
				</xsl:attribute>
				<xsl:attribute name="href">
					<xsl:value-of select="'ebforms.css'"/>
				</xsl:attribute>
			</xsl:element> 
		</xsl:if>

        <!-- set up the XForms model -->
        <!-- create instances for the main document and for the support documents -->
        <xf:model id="{$model.id}" schema="{$global.doc.schema.name}">
			<xsl:for-each select="msxsl:node-set($global.element.types.used)//ebforms:instance">
				<xsl:sort select="@level" />
				<xsl:choose>
					<xsl:when test="@mode='read'">
						<xf:instance id="{concat(@name,'-',@level)}" src="{@file}" readonly="true"/>
					</xsl:when>
					<xsl:otherwise>
					   <xf:instance id="{concat(@name,'-',@level)}"  > 
						   <xsl:element name="{$full.instance.name}" namespace="{$global.template.instance.ns}"> 
								<xsl:copy-of select="$global.template.instance.node/*/namespace::rsm" />
								<xsl:copy-of select="$global.template.instance.node/*/namespace::ram" />
								<xsl:copy-of select="$global.template.instance.node/*/namespace::sh" />
								<xsl:copy-of select="$global.template.instance.node/*/namespace::eanucc" />
								<xsl:copy-of select="$global.template.instance.node/*/namespace::order" />
								<xsl:if test="ebforms:schema/@show!='false'"> 
									<xsl:attribute name="schemaLocation" namespace="http://www.w3.org/2001/XMLSchema-instance" >
										<xsl:value-of select="$global.doc.schema.name" />
									</xsl:attribute> 
								</xsl:if> 

								<xsl:call-template name="write.instance">
									<xsl:with-param name="path" select="." />
								</xsl:call-template> 
						   </xsl:element>
						</xf:instance>
						
						
						<!-- write the submission definitions -->
						<xsl:for-each select="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=current()/ebforms:den]/ebforms:element[@class='document']/ebforms:submit" >
							<xsl:variable name="resource" select="@file" /> 
							<xf:submission id="{concat($instance.id,'-',@id)}" method="put" replace="none" includenamespaceprefixes="#default" instance="{$instance.id}" >
								<xf:resource value="{$resource}" /> 
								<xf:message ev:event="xforms-submit-error">
									<xsl:value-of select="@error" /> 
								</xf:message>
									<xsl:if test="@toggle-case!=''">
										<xf:toggle case="{@toggle-case}" ev:event="xforms-submit-done"/> 
									</xsl:if>
							</xf:submission> 
						</xsl:for-each>					
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
						
			<!-- create instances for the code lists -->
<!--			<xsl:variable name="enum.schema">
				<xsl:element name="root">  -->
<!--					<xsl:copy-of select="msxsl:node-set($global.element.types.used)/ebforms:instance[@name=$doc]//ebforms:enumSchema" /> -->
<!--					<xsl:copy-of select="msxsl:node-set($global.element.types.used)//ebforms:enumSchema" />
				</xsl:element> 
			</xsl:variable> -->
			
<!--			<xsl:for-each select="msxsl:node-set($enum.schema)//ebforms:enumSchema">  
				<xsl:if test="not(preceding-sibling::*=.)">
					<xsl:variable name="codelist-name">
						<xsl:call-template name="get.file.from.path">
							<xsl:with-param name="filepath"  select="." />
						</xsl:call-template>
					</xsl:variable>
					<xsl:element name="xf:instance">
						<xsl:attribute name="id"> -->
<!--							<xsl:value-of select="concat(substring-before($codelist-name,'.'),'-',$doc,'-',$level)" /> -->
	<!--						<xsl:value-of select="substring-before($codelist-name,'.')" />
						</xsl:attribute>
						<xsl:attribute name="src">
							<xsl:value-of select="concat($schemadir,$codelist-name)" />
						</xsl:attribute>
					</xsl:element>
				 </xsl:if>
			</xsl:for-each> -->
		
			<xsl:variable name="enumfiles">
				<xsl:for-each select="msxsl:node-set($global.element.types.used)//ebforms:enumSchema">  
					<xsl:variable name="codelist-name">
						<xsl:call-template name="get.file.from.path">
							<xsl:with-param name="filepath"  select="." />
						</xsl:call-template>
					</xsl:variable>
					<xsl:element name="xf:instance">
						<xsl:attribute name="id">  
<!--							<xsl:value-of select="concat(substring-before($codelist-name,'.'),'-',$doc,'-',$level)" /> -->
							<xsl:value-of select="substring-before($codelist-name,'.')" />
						</xsl:attribute>
						<xsl:attribute name="src">
							<xsl:value-of select="concat($schemadir,$codelist-name)" />
						</xsl:attribute>
						<xsl:attribute name="readonly">
							<xsl:value-of select="'true'" />
						</xsl:attribute>
					</xsl:element> 
				</xsl:for-each> 
<!--			<xsl:for-each select="msxsl:node-set($global.element.types.used)/ebforms:instance[@name=$doc and @level=$level]//ebforms:enum">  -->
				<xsl:for-each select="msxsl:node-set($global.element.types.used)//ebforms:enum">  
					<xsl:variable name="codelist-name">
						<xsl:call-template name="get.file.from.path">
							<xsl:with-param name="filepath"  select="@file" />
						</xsl:call-template>
					</xsl:variable>
					<xsl:element name="xf:instance">
						<xsl:attribute name="id">
							<xsl:value-of select="substring-before($codelist-name,'.')" />
						</xsl:attribute>
						<xsl:attribute name="src">
							<xsl:value-of select="@file" />
						</xsl:attribute>
						<xsl:attribute name="readonly">
							<xsl:value-of select="'true'" />
						</xsl:attribute>
					</xsl:element>
				</xsl:for-each> 
			</xsl:variable> 
						
			<xsl:for-each select="msxsl:node-set($enumfiles)/xf:instance">
				<xsl:if test="not(preceding-sibling::*/@id=./@id)">
					<xsl:copy-of select="." />
				</xsl:if>
			</xsl:for-each> 

			
			<!-- compose the bindings -->
			<xsl:variable name="calcnodesets">
				<xsl:for-each select="msxsl:node-set($global.element.types.used)//ebforms:element[(ebforms:mode='calc' and ebforms:calculate!='') or ebforms:constraint!='' or ebforms:required='true' or ((contains(@type,'DateType') or contains(@type,'xsd:date')) and (ebforms:Acronym='BBIE' or ebforms:Acronym='SC'))]">
					<xsl:element name="ebforms:binding">
						<xsl:attribute name="nodeset">
							<xsl:call-template name="getNodeset" >
								<xsl:with-param name="nodeset" select="ebforms:full-name" />
							</xsl:call-template>
						</xsl:attribute>
						<xsl:if test="ebforms:calculate!=''">
							<xsl:element name="ebforms:calculate">
								<xsl:value-of select="ebforms:calculate" />
							</xsl:element>
						</xsl:if>
						<xsl:if test="ebforms:constraint!=''">
							<xsl:element name="ebforms:constraint">
								<xsl:value-of select="ebforms:constraint" />
							</xsl:element>
						</xsl:if>
						<xsl:if test="ebforms:required='true'">
							<xsl:element name="ebforms:required">
								<xsl:value-of select="ebforms:required" />
							</xsl:element>
						</xsl:if>
						<xsl:if test="(contains(@type,'DateType') or @type='xsd:date') and (ebforms:Acronym='BBIE' or ebforms:Acronym='SC')"> 
							<xsl:element name="ebforms:isDate">
								<xsl:value-of select="'true'" />
							</xsl:element> 
						</xsl:if> 
					</xsl:element>				
				</xsl:for-each>
				<xsl:for-each select="msxsl:node-set($global.element.types.used)//ebforms:element/ebforms:attribute[(ebforms:mode='calc' and ebforms:calculate!='') or ebforms:constraint!='' or ebforms:required='true' or ((contains(@type,'DateType') or contains(@type,'xsd:date')) and (ebforms:Acronym='BBIE' or ebforms:Acronym='SC'))]">
					<xsl:element name="ebforms:binding">
						<xsl:attribute name="nodeset">
							<xsl:call-template name="getNodeset" >
								<xsl:with-param name="nodeset" select="concat('@',@name)" />
							</xsl:call-template>
						</xsl:attribute>
						<xsl:if test="ebforms:calculate!=''">
							<xsl:element name="ebforms:calculate">
								<xsl:value-of select="ebforms:calculate" />
							</xsl:element>
						</xsl:if>
						<xsl:if test="ebforms:constraint!=''">
							<xsl:element name="ebforms:constraint">
								<xsl:value-of select="ebforms:constraint" />
							</xsl:element>
						</xsl:if>
						<xsl:if test="ebforms:required='true'">
							<xsl:element name="ebforms:required">
								<xsl:value-of select="ebforms:required" />
							</xsl:element>
						</xsl:if>
						<xsl:if test="(contains(@type,'DateType') or @type='xsd:date') and (ebforms:Acronym='BBIE' or ebforms:Acronym='SC')">
							<xsl:element name="ebforms:isDate">
								<xsl:value-of select="'true'" />
							</xsl:element>
						</xsl:if> 
					</xsl:element>
				</xsl:for-each>
			</xsl:variable>
			
			<xsl:for-each select="msxsl:node-set($calcnodesets)/ebforms:binding[not(preceding-sibling::*/@nodeset=@nodeset)]">  
				<xsl:variable name="nodeset" select="@nodeset" />
				<xsl:element name="xf:bind">
					<xsl:attribute name="nodeset">
						<xsl:value-of select="$nodeset" />
					</xsl:attribute> 
					<xsl:if test="msxsl:node-set($calcnodesets)/ebforms:binding[@nodeset=$nodeset and ebforms:calculate!='']">
						<xsl:attribute name="calculate">
							<xsl:value-of select="msxsl:node-set($calcnodesets)/ebforms:binding[@nodeset=$nodeset]/ebforms:calculate" /> 
						</xsl:attribute>
					</xsl:if>
					<xsl:if test="msxsl:node-set($calcnodesets)/ebforms:binding[@nodeset=$nodeset and ebforms:constraint!='']">
						<xsl:attribute name="constraint">
							<xsl:value-of select="msxsl:node-set($calcnodesets)/ebforms:binding[@nodeset=$nodeset]/ebforms:constraint" /> 
						</xsl:attribute>
					</xsl:if>
					<xsl:if test="msxsl:node-set($calcnodesets)/ebforms:binding[@nodeset=$nodeset and ebforms:required!='']">
						<xsl:attribute name="required">
							<xsl:value-of select="1" /> 
						</xsl:attribute>
					</xsl:if>
					<xsl:if test="msxsl:node-set($calcnodesets)/ebforms:binding[@nodeset=$nodeset and ebforms:isDate!='']">
						<xsl:attribute name="type">
							<xsl:value-of select="'xsd:date'" /> 
						</xsl:attribute>
					</xsl:if>
				</xsl:element> 
			</xsl:for-each> 
			
			<!-- Set the default values -->
			<xsl:variable name="defnodesets">
				<xsl:for-each select="msxsl:node-set($global.element.types.used)//ebforms:instance[@mode!='read']//ebforms:element[ebforms:default/@calculate!='']">
					<xsl:element name="ebforms:binding">
						<xsl:attribute name="nodeset">
							<xsl:call-template name="getNodeset" >
								<xsl:with-param name="nodeset" select="ebforms:full-name" />
							</xsl:call-template>
						</xsl:attribute>
						<xsl:element name="ebforms:default">
							<xsl:value-of select="ebforms:default/@calculate" />
						</xsl:element>
					</xsl:element>					
				</xsl:for-each>
				<xsl:for-each select="msxsl:node-set($global.element.types.used)//ebforms:instance[@mode!='read']//ebforms:attribute[ebforms:default/@calculate!='']">
					<xsl:element name="ebforms:binding">
						<xsl:attribute name="nodeset">
							<xsl:call-template name="getNodeset" >
								<xsl:with-param name="nodeset" select="concat('@',@name)" />
							</xsl:call-template>
						</xsl:attribute>
						<xsl:element name="ebforms:default">
							<xsl:value-of select="ebforms:default/@calculate" />
						</xsl:element>
					</xsl:element>
				</xsl:for-each>
			</xsl:variable>
			<xsl:for-each select="msxsl:node-set($defnodesets)/ebforms:binding[not(preceding-sibling::*/@nodeset=@nodeset)]">  
				<xf:action ev:event="xforms-ready">
					<xf:setvalue ref="{@nodeset}" value="{ebforms:default}" />
				</xf:action>
				<xf:action ev:event="xforms-refresh" />
			</xsl:for-each>
			
			<!-- create an instance to hold search and select terms -->
			<xf:instance id="{'search'}" >
				<xsl:element name="search" namespace="http://code.google.com/p/eb-forms">
					<xsl:element name="All" namespace="http://code.google.com/p/eb-forms">
						<xsl:value-of select="'All'" />
					</xsl:element>
					<xsl:for-each select="msxsl:node-set($global.element.types.used)//ebforms:element/ebforms:search[.!='']|msxsl:node-set($global.element.types.used)//ebforms:attribute/ebforms:search[.!='']">
						<xsl:element name="{.}" namespace="http://code.google.com/p/eb-forms"/>
					</xsl:for-each>
					<xsl:for-each select="msxsl:node-set($global.element.types.used)//ebforms:element/ebforms:select[.!='']|msxsl:node-set($global.element.types.used)//ebforms:attribute/ebforms:select[.!=''] ">
						<xsl:element name="{.}" namespace="http://code.google.com/p/eb-forms">
							<xsl:value-of select="'All'" />
						</xsl:element>
					</xsl:for-each>
					<xsl:for-each select="msxsl:node-set($global.element.types.used)//ebforms:element[ebforms:show-number!='']">
						<xsl:element name="{@name}" namespace="http://code.google.com/p/eb-forms">
							<xsl:attribute name="from">
								<xsl:value-of select="'1'" />
							</xsl:attribute>
							<xsl:attribute name="to">
								<xsl:value-of select="ebforms:show-number" />
							</xsl:attribute>
							<xsl:attribute name="number">
								<xsl:value-of select="ebforms:show-number" />
							</xsl:attribute>
						</xsl:element>
					</xsl:for-each>
				</xsl:element> 
			</xf:instance> 
		</xf:model> 

    </head>
    <body>

	<!-- the eb-Forms logo -->
	<div id="headerleft"></div>
 
	<!-- generate the Xforms gui for all instances -->  
<xsl:for-each select="msxsl:node-set($global.element.types.used)/ebforms:instance[@level='1']">
	<xf:switch>
	  <xf:case id="{concat(msxsl:node-set($global.element.types.used)/ebforms:instance[@level='1']/@name,'-1-case')}" class="case" selected="true"> 
		   <xsl:for-each select="ebforms:instance[@level='3']">
				<xsl:call-template name="GenerateGui">
					<xsl:with-param name="instance.name" select="@name"  />
					<xsl:with-param name="instance.level" select="'3'"  />
				</xsl:call-template> 
			</xsl:for-each>
			<xsl:call-template name="GenerateGui">
				<xsl:with-param name="instance.name" select="@name"  />
				<xsl:with-param name="instance.level" select="'1'"  />
			</xsl:call-template>
		</xf:case>
		<xsl:for-each select="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=current()/ebforms:den]/ebforms:element[@class='document']/ebforms:submit/@toggle-case" >
			<xf:case id="{.}" class="case">
				<xsl:if test="../@toggle-message!=''">			
					<div class="{.}">
						<xf:output class="output" value="'{../@toggle-message}'" />
					</div>
				</xsl:if>	
			</xf:case>
		</xsl:for-each>		
		<xsl:for-each select="ebforms:instance[@level='2']">
		   <xf:case id="{concat(@name,'-2-case')}" class= "case" selected="'false'" >
			   <xsl:variable name="reply.name" select="@name" /> 
				<xsl:for-each select="ebforms:instance[@level='3']">
					<xsl:call-template name="GenerateGui">
						<xsl:with-param name="instance.name" select="@name"  />
						<xsl:with-param name="instance.level" select="'3'"  />
					</xsl:call-template>
				</xsl:for-each>
				<xsl:call-template name="GenerateGui">
					<xsl:with-param name="instance.name" select="$reply.name"  />
					<xsl:with-param name="instance.level" select="'2'"  />
				</xsl:call-template> 
			</xf:case> 
	
			<xsl:for-each select="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=current()/ebforms:den]/ebforms:element[@class='document']/ebforms:submit/@toggle-case" >
				<xf:case id="{.}" class="case">
					<xsl:if test="../@toggle-message!=''">			
						<div class="{.}">
							<xf:output class="output" value="'{../@toggle-message}'" />
						</div>
					</xsl:if>	
				</xf:case>
			</xsl:for-each>

		</xsl:for-each>
	</xf:switch>
</xsl:for-each>
</body>
</html>
</xsl:template>
 


 <!-- This template starts generation of the gui part from the XForms document. It creates a
		div for the root element of the message and then calls the template GuiElements to walk
		through the rest of the message.
        Context is instance in global.element.types.used -->
 
   <xsl:template name="GenerateGui">
    <xsl:param name="instance.name" />
    <xsl:param name="instance.level" />

    <!-- get reference to the instance that serves as template -->
    <xsl:variable name="ref">
		<xsl:text>instance( '</xsl:text>
		<xsl:value-of select="$instance.name" /> 
		<xsl:text>-</xsl:text>
		<xsl:value-of select="$instance.level" /> 
		 <xsl:text>' )</xsl:text>
    </xsl:variable>

    <xsl:variable name="selected">
		<xsl:choose>
			<xsl:when test="$instance.level=1">true()</xsl:when>
			<xsl:otherwise>false()</xsl:otherwise>
		</xsl:choose>
    </xsl:variable>

	<xsl:variable name="instance.mode">
		<xsl:choose>
			<xsl:when test="$instance.level=2">
				<xsl:value-of select="'new'" />
			</xsl:when>
			<xsl:when test="$instance.level=3">
				<xsl:value-of select="'read'" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$mode" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
    
    
<!--	<xsl:choose>
		<xsl:when test="$instance.level='1'">
			<xsl:for-each select="msxsl:node-set($global.element.types.used)/ebforms:instance[@level='3']" >
				<xsl:variable name="ref3">
					<xsl:text>instance( '</xsl:text>
					<xsl:value-of select="@name" /> 
					<xsl:text>-</xsl:text>
					<xsl:value-of select="@level" /> 
					<xsl:text>' )</xsl:text>
				</xsl:variable>
				<xsl:variable name="instance.name3" select="@name" />
				<xf:group ref="{$ref3}" class="{@strippedDen}" model="{@name}">
					<xf:label class="caption"><xsl:value-of select="ebforms:businessTerm"/></xf:label>
					
					<xsl:for-each select="ebforms:element[not(preceding-sibling::*/@name=@name)]">
						<xsl:sort select="ebforms:sequence" />
						<xsl:call-template name="GuiElements">
							<xsl:with-param name="showcaption" select="true()" />
							<xsl:with-param name="instance.mode" select="'read'" />
							<xsl:with-param name="instance.name" select="$instance.name3" />
							<xsl:with-param name="instance.level" select="'3'" />
						</xsl:call-template>
					</xsl:for-each>
				</xf:group>
			</xsl:for-each>
		</xsl:when>
	</xsl:choose> -->
  
 <xsl:if test="ebforms:show!='false'"> 

	<xsl:element name="xf:group">
		<xsl:attribute name="ref">
			<xsl:value-of select="$ref"/>
		</xsl:attribute>
		<xsl:if test="ebforms:class!='' or ebforms:width!=''">
			<xsl:attribute name="class">
				<xsl:if test="ebforms:width!=''">
					<xsl:value-of select="concat('W',ebforms:width)" />
					<xsl:if test="ebforms:class!=''">
						<xsl:value-of select="' '" />
					</xsl:if>
				</xsl:if>
				<xsl:value-of select="ebforms:class" />
			</xsl:attribute>
		</xsl:if>
		<xsl:attribute name="model">
			<xsl:value-of select="$model.id"/>
		</xsl:attribute>
    
		<xsl:if test="./ebforms:businessTerm!=''">
			<xsl:element name="xf:label">
				<xsl:if test="ebforms:caption-class!='' or ebforms:caption-width!=''">
					<xsl:attribute name="class">
						<xsl:if test="ebforms:caption-width!=''">
							<xsl:value-of select="concat('W',ebforms:caption-width)" />
							<xsl:if test="ebforms:caption-class!=''">
								<xsl:value-of select="' '" />
							</xsl:if>
						</xsl:if>
						<xsl:value-of select="ebforms:caption-class" />
					</xsl:attribute>
				</xsl:if>
				<xsl:value-of select="./ebforms:businessTerm"/>
			</xsl:element>
		</xsl:if>

<!-- Recursively write the controls for the message content -->

   <!--  <xsl:if test="ebform:show-children!='false'"> -->

     <xsl:for-each select="ebforms:element[not(preceding-sibling::*[1]/@name=@name)]"> 
 		<xsl:sort select="ebforms:sequence" />
			<xsl:if test="ebforms:show!='false'">		
				<xsl:call-template name="GuiElements">
					<xsl:with-param name="showcaption" select="true()" />
					<xsl:with-param name="instance.mode" select="$instance.mode" />
					<xsl:with-param name="instance.name" select="$instance.name" />
					<xsl:with-param name="instance.level" select="$instance.level" />
				</xsl:call-template>
			</xsl:if>
    </xsl:for-each>
    
  <!-- </xsl:if> -->  

    </xsl:element>
     
    <!-- buttons to reply docs or back --> 
	<div class="submissions" >
	 <xsl:choose>
		<xsl:when test="$instance.level=1">
		   	<xsl:for-each select="msxsl:node-set($global.element.types.used)/ebforms:instance[@name=$instance.name and @level='1']/ebforms:instance[@level='2']">
				<xsl:variable name="doc.id" select="concat(@name,'-2')" />
				<xf:trigger id="{$doc.id}" class="Save">
					<xf:label>
						<xsl:if test="ebforms:businessTerm!=''">
							<xsl:value-of select="ebforms:businessTerm" />
						</xsl:if>
					</xf:label>
					<xf:toggle case="{concat($doc.id,'-case')}" ev:event="DOMActivate" />
				</xf:trigger>
			</xsl:for-each>
			<xsl:if test="$mode!= 'read'">
				<xsl:for-each select="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=current()/ebforms:den]/ebforms:element[@class='document']/ebforms:submit">
					<xf:submit submission="{concat($instance.id,'-',@id)}" class="save" appearance="minimal">
						<xf:label>
							<xsl:choose>
								<xsl:when test="@button!=''">
									<img src="{@button}" />
								</xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="@caption" />
								</xsl:otherwise>
							</xsl:choose>
						</xf:label>	
					</xf:submit>
				</xsl:for-each>
			</xsl:if>
		</xsl:when>
		<xsl:when test="$instance.level=2">
			<xf:trigger id="{$instance.id}" class="save">
				<xf:label>
					<xsl:if test="substring-before($instance.id,'-')">
						<xsl:value-of select="substring-before($instance.id,'-')" />
					</xsl:if>
				</xf:label>
				<xf:toggle case="{concat($instance.id,'-case')}" ev:event="DOMActivate" />
			</xf:trigger>
			<xsl:if test="$mode!= 'read'">
				<for-each select="$global.annotations.node/ebforms:annotations/ebforms:core-component[@den=current()/ebforms:den]/ebforms:element[@class='document']/ebforms:submit">
					<xf:submit submission="{concat($instance.id,'-',@id)}" class="save" appearance="minimal">
						<xf:label>
							<img src="verzenden.gif" width="90" height="26" alt="Verzenden" />
							<xsl:if test="@button!=''">
								<xsl:value-of select="@button" />
							</xsl:if>
						</xf:label>
					</xf:submit>
				</for-each>
			</xsl:if>
		</xsl:when>
	</xsl:choose> 
   </div>
  </xsl:if> 
  
    <!-- The Close button -->
 <!--  <xsl:if test="$instance.level!=3" >
		<form method="post" class="close"> 
			<input type="button" class="save" value="Close" onclick="window.close()" />
		</form>  
	</xsl:if> -->
  </xsl:template>

	<!-- Generates the gui elements for each element found in the message -->
	<!-- Context is a node in global.element.types.used  -->
	<xsl:template name="GuiElements">
		<xsl:param name="showcaption" />
		<xsl:param name="instance.mode" />
		<xsl:param name="instance.name" />
		<xsl:param name="instance.level" />
				
	<!--	<xsl:variable name="name" select="@name" /> -->
		
		<xsl:variable name="element.mode">
			<xsl:choose>
				<xsl:when test="ebforms:mode='read'">
					<xsl:value-of select="'read'" />
				</xsl:when>
				<xsl:when test="ebforms:mode='calc'">
					<xsl:value-of select="'read'" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$instance.mode" />
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		
	<!--	<xsl:if test="$element.mode='read' or not(./preceding-sibling::*[@name=$name])"> -->

		<xsl:variable name="element.ref" select="ebforms:full-name" /> 
										
		<xsl:variable name="repeat.id">
			<xsl:value-of select="'repeat.'" />
			<xsl:value-of select="generate-id()" />
		</xsl:variable>

		<xsl:element name="xf:group">
			<xsl:if test="ebforms:class!='' or ebforms:width!=''">
				<xsl:attribute name="class">
					<xsl:if test="ebforms:width!=''">
						<xsl:value-of select="concat('W',ebforms:width)" />
						<xsl:if test="ebforms:class!=''">
							<xsl:value-of select="' '" />
						</xsl:if>
					</xsl:if>
					<xsl:value-of select="ebforms:class" />
				</xsl:attribute>
			</xsl:if>

			<xsl:if test="(ebforms:Acronym='ASBIE') and (not(@maxOccurs) or (@maxOccurs = 1))">
				<xsl:attribute name="ref">
					<xsl:value-of select="$element.ref" />
				</xsl:attribute> 
			</xsl:if>
			    
			<xsl:if test="ebforms:showcaption!='false' and $showcaption='true' and ebforms:businessTerm!=''">
				<xsl:element name="xf:label">
					<xsl:if test="ebforms:caption-class!='' or ebforms:caption-width!=''">
						<xsl:attribute name="class">
							<xsl:if test="ebforms:caption-width!=''">
								<xsl:value-of select="concat('W',ebforms:caption-width)" />
								<xsl:if test="ebforms:caption-class!=''">
									<xsl:value-of select="' '" />
								</xsl:if>
							</xsl:if>
							<xsl:value-of select="ebforms:caption-class" />
						</xsl:attribute>
					</xsl:if>
					<xsl:value-of select="ebforms:businessTerm" /> 
				</xsl:element>
			</xsl:if>

			<!-- hier nog toggle -->
	<!--		<xsl:if test="ebforms:show-children!='false'"> -->
				<xsl:variable name="search">
					<xsl:copy-of select="ebforms:search" />
					<xsl:copy-of select="ebforms:select" /> 
				</xsl:variable>

				<!-- repeating element, to be formatted as table -->
				<xsl:if test="((@maxOccurs = 'unbounded' or @maxOccurs > 1) and ebforms:control!='box') or ebforms:control='table'
												or (ebforms:Acronym='ASBIE' and not(ebforms:element/ebforms:Acronym='ASBIE') 
												    and (ebforms:element/@maxOccurs='unbounded' or ebforms:element/@maxOccurs > 1) and not(ebforms:control='box'))">
					<div class="head">
						<xsl:call-template name="write.header" />
					</div>
					<xsl:if test="ebforms:search!='' or ebforms:select!=''" >
						<div class="head">
							<xsl:call-template name="write.search" >
								<xsl:with-param name="search" select="$search" />
								<xsl:with-param name="repeat-name" select="$repeat.id" />
							</xsl:call-template> 
						</div>
					</xsl:if>
				</xsl:if> 
				<!-- table header -->
			
				<xsl:variable name="showcap">
					<xsl:choose>
						<xsl:when test="$showcaption='false' or (((@maxOccurs = 'unbounded' or @maxOccurs > 1) and ebforms:control!='box') or ebforms:control='table'
													or (ebforms:Acronym='ASBIE' and not(ebforms:element/ebforms:Acronym='ASBIE') and not(ebforms:control='box')))">
							 <xsl:value-of select="false()" />
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="true()" />
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
						
				<xsl:choose>
					<xsl:when test="(@maxOccurs = 'unbounded') or (@maxOccurs > 1)">
					
	<!--					<xsl:variable name="repeat.id">
							<xsl:value-of select="ebforms:strippedDen" />
						</xsl:variable> -->
							
						<xsl:variable name="searchref">
							<xsl:value-of select="$element.ref" />
							<xsl:call-template name="make.searchref">
								<xsl:with-param name="search" select="$search" />
								<xsl:with-param name="path" select="''"/>
							</xsl:call-template>
						</xsl:variable>
					 
						<xsl:variable name="valuepath">
							<xsl:call-template name="FindValuePath">
								<xsl:with-param name="valuepath" select="$searchref" />
								<xsl:with-param name="searchpath" select=".."/>
								<xsl:with-param name="repeat.id" select="$repeat.id" />
							</xsl:call-template>
						</xsl:variable>
					
						<div class="lines">
					
							<xf:repeat nodeset="{$searchref}" id="{$repeat.id}"  >	
							
<!--							<xsl:element name="xf:repeat">
								<xsl:attribute name="nodeset">
									<xsl:value-of select="$searchref"	/>					
								</xsl:attribute>
								<xsl:attribute name="id">
									<xsl:value-of select="$repeat.id"	/>					
								</xsl:attribute>
								<xsl:if test="ebforms:show-number!=''" >
									<xsl:attribute name="number">
										<xsl:value-of select="ebforms:show-number"	/>					
									</xsl:attribute>
								</xsl:if> -->
														
								<div class="line-full">

									<xsl:if test="$element.mode='new' and ebforms:del-button!='false'" >
										<div class="del">
											<xf:trigger class="del" appearance="minimal">
												<xf:label><img src="del.gif" alt="Del"/></xf:label>
												<xf:action ev:event="DOMActivate">
														<xsl:for-each select="ebforms:element">
															<xsl:call-template name="get.calculated.default">
																<xsl:with-param name="path" select="msxsl:node-set(concat('../',$element.ref,'[last()=1]','/',ebforms:full-name))"/>
															</xsl:call-template>
														</xsl:for-each>
													<xf:delete nodeset="{concat('../',$element.ref,'[last()&gt;1]')}" at="index('{$repeat.id}')" />
												</xf:action>
											</xf:trigger>
										</div>
									</xsl:if>
	
									<xsl:if test="ebforms:add-path!=''">
									
<!--										<xsl:variable name="repeat.name" >
											<xsl:call-template name="GetStrippedDen">
												<xsl:with-param name="den" select="ebforms:add-path/@target"/>
											</xsl:call-template>
										</xsl:variable> -->
										
										<xsl:variable name="add-path" select="ebforms:add-path" />
										<xsl:variable name="test-empty">
											<xsl:choose>
												<xsl:when test="ebforms:add-path/@test-empty!=''">
													<xsl:value-of select="ebforms:add-path/@test-empty"	/>					
												</xsl:when>
												<xsl:otherwise>
													<xsl:value-of select="'*'"	/>					
												</xsl:otherwise>
											</xsl:choose>
										</xsl:variable>
	
										<xf:trigger class="copy" appearance="minimal">
											<xf:label><img src="copy.gif" alt="Copy" /></xf:label>
											<xf:action ev:event="DOMActivate">
												<xsl:choose>
													<xsl:when test="ebforms:add-path/@position='first'">
														<xf:insert nodeset="{concat('instance(',$quot,$global.template.instance.name,'-1',$quot,')/',$add-path)}" at="1" position="before" />
														<xsl:variable name="target" select="ebforms:add-path/@target" />
														<xsl:for-each select="//ebforms:element[ebforms:den=$target]">
															<xsl:call-template name="get.calculated.default">
																<xsl:with-param name="path" select="concat('instance(',$quot,$global.template.instance.name,'-1',$quot,')/',$add-path,'[1]')"/>
															</xsl:call-template>
														</xsl:for-each> 
														
													</xsl:when>
													<xsl:when test="ebforms:add-path/@position='before'">
														<xf:insert nodeset="{concat('instance(',$quot,$global.template.instance.name,'-1',$quot,')/',$add-path)}" at="index({concat($quot,$repeat.id,$quot)})" position="before"  />
							<!--							<xsl:for-each select="//ebforms:element[ebforms:den=ebforms:add-path/@target]">
															<xsl:call-template name="get.calculated.default">
																<xsl:with-param name="path" select="concat('instance(',$quot,$global.template.instance.name,'-1',$quot,')/',$add-path,'[',index($repeat.id),']')"/>
															</xsl:call-template>
														</xsl:for-each> -->
			
													</xsl:when>
													<xsl:when test="ebforms:add-path/@position='after'">
														<xf:insert nodeset="{concat('instance(',$quot,$global.template.instance.name,'-1',$quot,')/',$add-path)}" at="index({concat($quot,$repeat.id,$quot)})" position="after"  />
						<!--								<xsl:for-each select="//ebforms:element[ebforms:den=ebforms:add-path/@target]">
															<xsl:call-template name="get.calculated.default">
																<xsl:with-param name="path" select="concat('instance(',$quot,$global.template.instance.name,'-1',$quot,')/',$add-path,'[',index($repeat.id)+1,']')"/>
															</xsl:call-template>
														</xsl:for-each> -->
			
													</xsl:when>
													<xsl:otherwise>
														<xf:insert nodeset="{concat('instance(',$quot,$global.template.instance.name,'-1',$quot,')/',$add-path)}" at="last()" position="after"  />
							<!--							<xsl:for-each select="//ebforms:element[ebforms:den=ebforms:add-path/@target]">
															<xsl:call-template name="get.calculated.default">
																<xsl:with-param name="path" select="concat('instance(',$quot,$global.template.instance.name,'-1',$quot,')/',$add-path,'[','last()',']')"/>
															</xsl:call-template>
														</xsl:for-each> -->
			
													</xsl:otherwise>
												</xsl:choose>
												<xsl:call-template name="setvalue">
													<xsl:with-param name="targetpath" select="$add-path" />
													<xsl:with-param name="valuepath" select="concat($valuepath,'[index(',$quot,$repeat.id,$quot,')]')" />
													<xsl:with-param name="instance" select="concat($instance.name,'-',$instance.level)" />
													<xsl:with-param name="position" select="ebforms:add-path/@position" />
													<xsl:with-param name="repeat.name" select="$repeat.id" />
												</xsl:call-template> 
											
												<xf:delete nodeset="{concat('instance(',$quot,$global.template.instance.name,'-1',$quot,')/',$add-path,'[',$test-empty,'=',$quot,$quot,']')}" />
											</xf:action>
										</xf:trigger>
									</xsl:if>
								
									<div class="line">
		
										<xsl:choose>
											<xsl:when test="ebforms:Acronym='BBIE'">
												 <xsl:call-template name="HandleDataType">
													<xsl:with-param name="ref" select="'.'" />
													<xsl:with-param name="showcaption" select="$showcap" />
													<xsl:with-param name="element.mode" select="$element.mode" />
													<xsl:with-param name="instance.name" select="$instance.name" />
													<xsl:with-param name="instance.level" select="$instance.level" />
												 </xsl:call-template>
											</xsl:when>
											<xsl:otherwise>
												<xsl:for-each select="ebforms:element[not(preceding-sibling::*[1]/@name=@name)]">
													<xsl:sort select="ebforms:sequence" />
														<xsl:if test="ebforms:show!='false'">		
															<xsl:call-template name="GuiElements" >
																<xsl:with-param name="showcaption" select="$showcap" />
																<xsl:with-param name="instance.mode" select="$element.mode" />
																<xsl:with-param name="instance.name" select="$instance.name" />
																<xsl:with-param name="instance.level" select="$instance.level" />
															</xsl:call-template>
														</xsl:if>
												</xsl:for-each>
											</xsl:otherwise>
										</xsl:choose>	
										<xsl:if test="ebforms:field!=''">
											<div class="field">
												<xsl:choose>
													<xsl:when test="ebforms:field/@width!=''">
														<xf:output value="{ebforms:field/@calculate}" class="{concat('W',field/@width,' ','output')}"></xf:output>
													</xsl:when>
													<xsl:otherwise>
														<xf:output value="{ebforms:field/@calculate}" class="{concat('W24',' ','output')}"></xf:output>
													</xsl:otherwise>
												</xsl:choose>
											</div>
										</xsl:if>			
									</div>	
								</div>
								<!-- line -->
						
							</xf:repeat>
							<!-- </xf:repeat> -->
							
							<xsl:if test="ebforms:show-number!=''" >
								<div class="paging">
									<xf:trigger class="previous" appearance="minimal">
										<xf:label><img src="previous.gif" width="30" height="15" alt="Previous" /></xf:label>
										<xf:action ev:event="DOMActivate">
											<xf:setvalue ref="{concat('instance(',$quot,'search',$quot,')/ebforms:',@name,'/@from')}" value="{concat('if(instance(',$quot,'search',$quot,')/ebforms:',@name,'/@from&gt;instance(',$quot,'search',$quot,')/ebforms:',@name,'/@number',$comma,'instance(',$quot,'search',$quot,')/ebforms:',@name,'/@from - instance(',$quot,'search',$quot,')/ebforms:',@name,'/@number',$comma,'1)')}" />
											<xf:setvalue ref="{concat('instance(',$quot,'search',$quot,')/ebforms:',@name,'/@to')}" 
		value="{concat('if(instance(',$quot,'search',$quot,')/ebforms:',@name,'/@from + instance(',$quot,'search',$quot,')/ebforms:',@name,'/@number&lt;count(',substring-before(concat('instance(',$quot,$instance.name,'-',$instance.level,$quot,')/',$valuepath),'[position'),')',$comma,'instance(',$quot,'search',$quot,')/ebforms:',@name,'/@from + instance(',$quot,'search',$quot,')/ebforms:',@name,'/@number - 1',$comma, 'count(',substring-before(concat('instance(',$quot,$instance.name,'-',$instance.level,$quot,')/',$valuepath),'[position'),'))')}" /> 

											<xf:setindex repeat="{$repeat.id}" index="'1'" />
											<xf:rebuild model="{$model.id}" />
<!--											<xf:setindex repeat="{$repeat.id}" index="{concat('instance(',$quot,'search',$quot,')/ebforms:',@name,'/@from')}" /> -->
								<!--		<xf:setindex repeat="{$repeat.id}" index="index('{$repeat.id}') - {ebforms:show-number}" /> -->
										</xf:action>							
									</xf:trigger>
<!--										<xf:output class="output" value="concat(index('{$repeat.id}'),' van ',count-non-empty({concat('instance(',$quot,$instance.name,'-',$instance.level,$quot,')/',$valuepath)}))" /> -->


								<xf:output class="output" value="concat({concat('instance(',$quot,'search',$quot,')/ebforms:',@name,'/@from')},' - ',{concat( 'instance(',$quot,'search',$quot,')/ebforms:',@name,'/@to')},{concat($quot,' ',ebforms:paging-caption,' ',$quot)},count({substring-before(concat('instance(',$quot,$instance.name,'-',$instance.level,$quot,')/',$valuepath),'[position')}))" />
								
							<!--			<xf:output value="'_van_'" />
										<xf:output value="count-non-empty({concat('instance(',$quot,$instance.name,'-',$instance.level,$quot,')/',$valuepath)})" />  -->
									<xf:trigger class="next" appearance="minimal">
										<xf:label><img src="next.gif" width="30" height="15" alt="Next" /></xf:label>
										<xf:action ev:event="DOMActivate">
											<xf:setvalue ref="{concat('instance(',$quot,'search',$quot,')/ebforms:',@name,'/@to')}" value="{concat('if(instance(',$quot,'search',$quot,')/ebforms:',@name,'/@to&lt;count(',substring-before(concat('instance(',$quot,$instance.name,'-',$instance.level,$quot,')/',$valuepath),'[position'),') - instance(',$quot,'search',$quot,')/ebforms:',@name,'/@number',$comma, 'instance(',$quot,'search',$quot,')/ebforms:',@name,'/@to + instance(',$quot,'search',$quot,')/ebforms:',@name,'/@number',$comma,'count(',substring-before(concat('instance(',$quot,$instance.name,'-',$instance.level,$quot,')/',$valuepath),'[position'),'))')}" /> 
	
											<xf:setvalue ref="{concat('instance(',$quot,'search',$quot,')/ebforms:',@name,'/@from')}" value="{concat('if(instance(',$quot,'search',$quot,')/ebforms:',@name,'/@to&gt;instance(',$quot,'search',$quot,')/ebforms:',@name,'/@number',$comma,'instance(',$quot,'search',$quot,')/ebforms:',@name,'/@to - instance(',$quot,'search',$quot,')/ebforms:',@name,'/@number + 1',$comma,'1)')}" />

											<xf:setindex repeat="{$repeat.id}" index="'1'" />
											<xf:rebuild model="{$model.id}" /> 
<!--											<xf:setindex repeat="{$repeat.id}" index="{concat('instance(',$quot,'search',$quot,')/ebforms:',@name,'/@from')}" /> -->
										<!--	<xf:setindex repeat="{$repeat.id}" index="index('{$repeat.id}') + {ebforms:show-number}" /> -->
										</xf:action>
									</xf:trigger>
								</div>
							</xsl:if>
						</div>
						<!-- lines -->

						<xsl:if test="$element.mode!='read' and ebforms:add-button!='false'" >
							<div class="add">
								<xf:trigger class="new" appearance="minimal">
									<xf:label><img src="new.gif" alt="New"/></xf:label>
									<xf:action ev:event="DOMActivate">
										<xf:insert nodeset="{$element.ref}" position="after" at="index('{$repeat.id}')">
										</xf:insert>
										<xsl:if test="$global.xfprocessor='FP'">
											<xf:setindex repeat="{$repeat.id}" index="index('{$repeat.id}') + 1" />
										</xsl:if>
										<xsl:for-each select="ebforms:element">
											<xsl:call-template name="get.calculated.default">
												<xsl:with-param name="path" select="msxsl:node-set(concat($element.ref,'[index(',$quot,$repeat.id,$quot,')]','/',ebforms:full-name))"/>
											</xsl:call-template>
										</xsl:for-each>
								<!--		<xf:delete nodeset="{concat($element.ref,'[*=',$quot,$quot,']')}" /> -->
										<xsl:if test="@maxOccurs!='unbounded'">
											<xf:delete nodeset="{concat($element.ref,'[position()=',number(@maxOccurs) +1,']')}" />
										</xsl:if>
									</xf:action>
								</xf:trigger>
							</div>
						</xsl:if> 
			
							
		<!--					<div class="del">
								<xf:trigger class="del" appearance="minimal">
									<xf:label><img src="del.gif" width="15" height="15" alt="Del"/></label>
									
									<xf:action ev:event="DOMActivate">
										
															<xsl:for-each select="ebforms:element">
																<xsl:call-template name="get.calculated.default">
																	<xsl:with-param name="path" select="msxsl:node-set(concat($element.ref,'[last()=1]','/',@name))"/>
																</xsl:call-template>
															</xsl:for-each>
									
										<xf:delete nodeset="{concat($element.ref,'[last()>1]')}" at="index('{$repeat.id}')" />
									</xf:action>
								</xf:trigger>
							</div> -->
						<!-- mode=read -->
					</xsl:when>
					<!-- repeating element -->

					<!-- non-repeating BBIE -->
					<xsl:when test="ebforms:Acronym='BBIE'">
							<xsl:call-template name="HandleDataType">
							<xsl:with-param name="ref" select="$element.ref" />
							<xsl:with-param name="showcaption" select="$showcaption" />
							<xsl:with-param name="element.mode" select="$element.mode" />
							<xsl:with-param name="instance.name" select="$instance.name" />
							<xsl:with-param name="instance.level" select="$instance.level" />
						</xsl:call-template>
					</xsl:when>
					          					
					<!-- non-repeating ASBIE -->
					<xsl:otherwise>
						<xsl:if test="ebforms:show-children!='false'">
							<xsl:for-each select="ebforms:element[not(preceding-sibling::*[1]/@name=@name)]">
								<xsl:sort select="ebforms:sequence" />
								<xsl:if test="ebforms:show!='false'">		
									<xsl:call-template name="GuiElements" >
										<xsl:with-param name="showcaption" select="$showcaption" />
										<xsl:with-param name="instance.mode" select="$element.mode" />
										<xsl:with-param name="instance.name" select="$instance.name" />
										<xsl:with-param name="instance.level" select="$instance.level" />
									</xsl:call-template>
								</xsl:if>
							</xsl:for-each>
						</xsl:if>
		<!--				<xsl:if test="ebforms:field!=''">
							<xf:output value="{ebforms:field/@calculate}" class="output">
								<xf:label><xsl:value-of select="ebforms:field/@caption" /></xf:label>
							</xf:output>
						</xsl:if>		-->	
					</xsl:otherwise>
				</xsl:choose>	
			
				<xsl:if test="ebforms:image!=''">
					<div class="image">
						<img src="{ebforms:image}" alt="{substring-before(ebforms:image,'.')}"  />
					</div>
				</xsl:if>
			
	<!--		<xsl:for-each select="ebforms:attribute">
				 <xsl:call-template name="HandleDataType">
					<xsl:with-param name="ref" select="$element.ref" />
					<xsl:with-param name="showcaption" select="$showcaption" />
					<xsl:with-param name="element.mode" select="$element.mode" />
					<xsl:with-param name="instance.name" select="$instance.name" />
					<xsl:with-param name="instance.level" select="$instance.level" />
				</xsl:call-template>
			</xsl:for-each> -->
			
<!--		</xsl:if> --> 
		</xsl:element>
	<!--	</xsl:if> -->
  </xsl:template>

  
  	<xsl:template name="get.calculated.default">
		<xsl:param name="path"/>
		<xsl:if test="ebforms:show!='false'">
			<xsl:choose>
				<xsl:when test="ebforms:default!=''">
					<xf:setvalue ref="{$path}" value="{concat($quot,ebforms:default,$quot)}" />
				</xsl:when>
				<xsl:when test="ebforms:default/@calculate!=''">
					<xf:setvalue ref="{$path}" value="{ebforms:default/@calculate}" />
				</xsl:when>
				<xsl:when test="ebforms:Acronym='BBIE' or ebforms:Acronym='SC'">
					<xf:setvalue ref="{$path}" value="''" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:if test="not(ebforms:show-children) or ebforms:show-children!='false'">
						<xsl:for-each select="ebforms:element">
							<xsl:call-template name="get.calculated.default" >
								<xsl:with-param name="path" select="concat($path,'/',ebforms:full-name)"/>
							</xsl:call-template>
						</xsl:for-each>
					</xsl:if>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:if test="not(ebforms:show-children) or ebforms:show-children!='false'">
				<xsl:for-each select="ebforms:attribute">
					<xsl:call-template name="get.calculated.default" >
						<xsl:with-param name="path" select="concat($path,'/@',ebforms:full-name)"/>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:if>
		</xsl:if> 
	</xsl:template> 
  
<xsl:template name="write.header">
	<xsl:for-each select="ebforms:element[not(preceding-sibling::*[1]/@name=@name)]" >
	 	<xsl:sort select="ebforms:sequence" />
		<xsl:if test="ebforms:showcaption!='false' and ebforms:show!='false'">		
			<xsl:element name="div">
				<xsl:if test="ebforms:width!=''">
					<xsl:attribute name="class">
						<xsl:value-of select="concat('W',ebforms:width)" />
					</xsl:attribute>
				</xsl:if>
				<xsl:element name="div">
					<xsl:if test="ebforms:caption-class!=''">
						<xsl:attribute name="class">
							<xsl:value-of select="ebforms:caption-class" />
						</xsl:attribute>
					</xsl:if>
					<xsl:if test="ebforms:businessTerm!=''">				
						<xsl:value-of select="ebforms:businessTerm"/>
					</xsl:if>
				</xsl:element>
			</xsl:element>
			<xsl:for-each select="ebforms:attribute" >
				<xsl:if test="ebforms:showcaption!='false' and ebforms:show!='false'">
					<xsl:element name="div">
						<xsl:if test="ebforms:width!=''">
							<xsl:attribute name="class">
								<xsl:value-of select="concat('W',ebforms:width)" />
							</xsl:attribute>
						</xsl:if>
						<xsl:element name="div">
							<xsl:if test="ebforms:caption-class!=''">
								<xsl:attribute name="class">
									<xsl:value-of select="ebforms:caption-class" />
								</xsl:attribute>
							</xsl:if>
							<xsl:if test="ebforms:businessTerm!=''">				
								<xsl:value-of select="ebforms:businessTerm"/>
							</xsl:if>
						</xsl:element>
					</xsl:element>
				</xsl:if> 
			</xsl:for-each>
			<xsl:call-template name="write.header" />
		</xsl:if> 
	</xsl:for-each> 
	
	<xsl:if test="ebforms:field!=''">
		<div class="field">
			<div class="caption">
				<xsl:value-of select="ebforms:field/@caption" />
			</div>
		</div>
	</xsl:if>
</xsl:template>

<xsl:template name="write.search">
	<xsl:param name="search" />
	<xsl:param name="repeat-name" />
	
	<xsl:for-each select="ebforms:element[not(preceding-sibling::*[1]/@name=@name)]" >
		<xsl:sort select="ebforms:sequence" />
		<xsl:if test="ebforms:show!='false'">	
			<xsl:variable name="prefix">
				<xsl:value-of select="''"/>
				<xsl:if test="ebforms:prefix!=''">
					<xsl:value-of select="'eb'"/>
					<xsl:value-of select="ebforms:prefix"/>
					<xsl:value-of select="':'"/>
				</xsl:if>
			</xsl:variable>
			<xsl:element name="div">
				<xsl:if test="ebforms:width!=''">
					<xsl:attribute name="class">
						<xsl:value-of select="concat('W',ebforms:width)" />
					</xsl:attribute>
				</xsl:if>
				<xsl:choose>
					<xsl:when test="msxsl:node-set($search)/ebforms:select=@name">
						<div class="select">
							<xsl:element name="xf:select1">
								<xsl:attribute name="ref">
									<xsl:value-of select="concat('instance(',$quot,'search',$quot,')/','ebforms:',@name)" />
								</xsl:attribute>
								<xsl:attribute name="appearance">
									<xsl:value-of select="'minimal'" />
								</xsl:attribute>
								<xsl:attribute name="class">
									<xsl:value-of select="'select1'" />
									<xsl:if test="ebforms:content-width!=''">
										<xsl:value-of select="' '" />
										<xsl:value-of select="concat('W',ebforms:content-width)" />
									</xsl:if>
								</xsl:attribute>
	<!--						<xf:select1 ref="{concat('instance(',$quot,'search',$quot,')/',@name)}" class="select1" appearance="minimal"> -->
								<xf:itemset nodeset="{concat('instance(',$quot,'search',$quot,')/','ebforms:All','|','instance(',$quot,msxsl:node-set($search)/ebforms:select/@instance,$quot,')//',								$prefix,msxsl:node-set($search)/ebforms:select,'[not(preceding::',$prefix,msxsl:node-set($search)/ebforms:select,'=.)]')}" > 
									<xf:label ref="." /> 
									<xf:value ref="." />
								</xf:itemset> 
								<xf:action ev:event="xforms-value-changed">
									<xf:rebuild model="{$model.id}" />
									<xf:setindex repeat="{$repeat-name}" index="1" />
								</xf:action>
								<xsl:if test="msxsl:node-set($search)/ebforms:select/@caption!=''">
									<xsl:element name="xf:label">
										<xsl:attribute name="class">
											<xsl:value-of select="'select-label'" /> 
												<xsl:if test="ebforms:caption-class!=''" >
													<xsl:value-of select="' '" /> 
													<xsl:value-of select="ebforms:caption-class" /> 
												</xsl:if>
												<xsl:if test="ebforms:caption-width!=''" >
													<xsl:value-of select="' W'" /> 
													<xsl:value-of select="ebforms:caption-width" /> 
												</xsl:if>
										</xsl:attribute>
										<xsl:value-of select="msxsl:node-set($search)/ebforms:select/@caption" /> 
									</xsl:element>
								</xsl:if>
							</xsl:element>
						</div> 
					</xsl:when>
					<xsl:when test="msxsl:node-set($search)/ebforms:search=@name">
						<div class="search">
							<xsl:element name="xf:input">
								<xsl:attribute name="ref">
									<xsl:value-of select="concat('instance(',$quot,'search',$quot,')/','ebforms:',@name)" />
								</xsl:attribute>
								<xsl:attribute name="class">
									<xsl:value-of select="'input'" />
									<xsl:if test="ebforms:content-width!=''">
										<xsl:value-of select="' '" />
										<xsl:value-of select="concat('W',ebforms:content-width)" />
									</xsl:if>
								</xsl:attribute>
<!--								<xf:input ref="{concat('instance(',$quot,'search',$quot,')/',@name)}" class="input" > -->
								<xf:action ev:event="xforms-value-changed">
									<xf:rebuild model="{$model.id}" />
									<xf:setindex repeat="{$repeat-name}" index="1" />
								</xf:action>
								<xsl:if test="msxsl:node-set($search)/ebforms:search/@caption!=''">
									<xsl:element name="xf:label">
										<xsl:attribute name="class">
											<xsl:value-of select="'search-label'" /> 
												<xsl:if test="ebforms:caption-class!=''" >
													<xsl:value-of select="' '" /> 
													<xsl:value-of select="ebforms:caption-class" /> 
												</xsl:if>
												<xsl:if test="ebforms:caption-width!=''" >
													<xsl:value-of select="' W'" /> 
													<xsl:value-of select="ebforms:caption-width" /> 
												</xsl:if>
										</xsl:attribute>
										<xsl:value-of select="msxsl:node-set($search)/ebforms:search/@caption" /> 
									</xsl:element>								</xsl:if>
							</xsl:element> 
						</div>
					</xsl:when> 
			<!--  <xsl:when test="ebforms:search/@caption!=''">
						<xf:label class="{ebforms:caption-class}">
							<xsl:value-of select="ebforms:search/@caption" /> 
						</xf:label>
					</xsl:when>	--> 			
				</xsl:choose>
				<xsl:call-template name="write.search">
					<xsl:with-param name="search" select="$search"/>
					<xsl:with-param name="repeat-name" select="$repeat-name" />
				</xsl:call-template> 
			</xsl:element>
		</xsl:if>
	</xsl:for-each>			
</xsl:template>
							
<xsl:template name="make.searchref">
	 	<xsl:param name="search" />
		<xsl:param name="path" />
		<xsl:for-each select="ebforms:element[not(preceding-sibling::*[1]/@name=@name)]" >
			<xsl:sort select="ebforms:sequence" />
			<xsl:if test="ebforms:show!='false'">	
				<xsl:choose>
					<xsl:when test="msxsl:node-set($search)/ebforms:select=@name">
						<xsl:value-of select="concat('[',$path,ebforms:full-name,'=','instance(',$quot,'search',$quot,')/','ebforms:',@name,' or ','instance(',$quot,'search',$quot,')/','ebforms:',@name,'=',$quot,'All',$quot,']')"/> 
					</xsl:when>
					<xsl:when test="msxsl:node-set($search)/ebforms:search=@name">
						<xsl:value-of select="concat('[contains(','translate(',$path,ebforms:full-name,$comma,$quot,'abcdefghijklmnopqrstuvwxyz',$quot,$comma,$quot,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',$quot,')',$comma,'translate(','instance(',$quot,'search',$quot,')/','ebforms:',@name,$comma,$quot,'abcdefghijklmnopqrstuvwxyz',$quot,$comma,$quot,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',$quot,'))]')" />
					</xsl:when> 
				</xsl:choose> 
				<xsl:call-template name="make.searchref">
					<xsl:with-param name="search" select="$search" />
					<xsl:with-param name="path" select="concat($path,ebforms:full-name,'/')"/> 
				</xsl:call-template> 
			</xsl:if>		
		</xsl:for-each>
<!--		<xsl:if test="ebforms:show-number!='' and $global.xfprocessor!='FP'"> -->
		<xsl:if test="ebforms:show-number!=''" >
			<xsl:value-of select="concat('[position() &gt;= ','instance(',$quot,'search',$quot,')/','ebforms:',@name,'/@from and position() &lt;= ','instance(',$quot,'search',$quot,')/','ebforms:',@name,'/@to]')" />
		</xsl:if>			
</xsl:template>		
<!-- 
		Context is an ebforms:element in global.element.types.used with acronym = BBIE.
	-->
  <xsl:template name="HandleDataType">
    <xsl:param name="ref" />
    <xsl:param name="showcaption" />
    <xsl:param name="element.mode" />
	<xsl:param name="instance.name" />
	<xsl:param name="instance.level" />

    <xsl:variable name="element.type.schema" select="ebforms:schema" />
	<xsl:variable name="doc" select="ancestor::ebforms:instance/@name" />
	<xsl:variable name="level" select="ancestor::ebforms:instance/@level" />
    <xsl:variable name="den" select="ebforms:den" />

    <xsl:variable name="displayLabel" >
		<xsl:choose>
			<xsl:when 
				test="$showcaption='false' or $global.annotations.node/ebforms:annotations/
					ebforms:core-component[@den = $den]/ebforms:element[@class='caption']/ebforms:show[@value='false']">
				<xsl:value-of select="false()"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="true()" />
			</xsl:otherwise>
		</xsl:choose>
    </xsl:variable>
    
    <xsl:choose>
    	
		<xsl:when test="$element.mode='read' and ebforms:enumSchema!=''">
			<xsl:variable name="basetype" select="ebforms:enumSchema/@basetype" />
			<xsl:variable name="default" select="ebforms:default" />
			<xsl:variable name="enumschema">
				<xsl:value-of select="$schemadir" />
				<xsl:call-template name="get.file.from.path">
					<xsl:with-param name="filepath" select="ebforms:enumSchema" />
				</xsl:call-template>
			</xsl:variable>
			 <xsl:variable name="codename" select="document($enumschema)//xsd:simpleType[@name=$basetype or @name=substring-after($basetype,':')]				/xsd:restriction/xsd:enumeration[@value=$default]/xsd:annotation/xsd:documentation/ccts:Name" /> 
			<xsl:element name="xf:output">
				<xsl:attribute name="value">
					<xsl:value-of select="concat($quot,$codename,$quot)"/>
				</xsl:attribute>
				<xsl:if test="ebforms:content-class!='' or ebforms:content-width!=''">
					<xsl:attribute name="class">
						<xsl:if test="ebforms:content-width!=''">
							<xsl:value-of select="concat('W',ebforms:content-width)" />
								<xsl:if test="ebforms:content-class!=''">
									<xsl:value-of select="' '" />
								</xsl:if>
						</xsl:if>
						<xsl:value-of select="ebforms:content-class" />
					</xsl:attribute>
				</xsl:if>
				<xsl:attribute name="model">
					<xsl:value-of select="$model.id"/>
				</xsl:attribute>
				<xsl:if test="$displayLabel='true' and $showcaption='true' and ebforms:businessTerm!=''">
					<xsl:element name="xf:label">
						<xsl:if test="ebforms:caption-class!='' or ebforms:caption-width!=''">
							<xsl:attribute name="class">
								<xsl:if test="ebforms:caption-width!=''">
									<xsl:value-of select="concat('W',ebforms:caption-width)" />
										<xsl:if test="ebforms:caption-class!=''">
									<xsl:value-of select="' '" />
								</xsl:if>
								</xsl:if>
								<xsl:value-of select="ebforms:caption-class" />
							</xsl:attribute>
						</xsl:if>
						<xsl:value-of select="ebforms:businessTerm" />
					</xsl:element>
				</xsl:if>
			</xsl:element>
    	</xsl:when>
    	
  		<xsl:when test="$element.mode='read' and ebforms:enum/@file!=''">
  			<xsl:variable name="enumfile">
				<xsl:call-template name="get.file.from.path">
					<xsl:with-param name="filepath" select="ebforms:enum/@file" />
				</xsl:call-template>
			</xsl:variable>
			<xsl:element name="xf:output">
				<xsl:attribute name="value">
					<xsl:value-of select="concat('instance(',$quot,substring-before($enumfile,'.'),$quot,')',ebforms:enum/@nodepath,'[',ebforms:enum/@valuepath,'=',$quot,ebforms:default,$quot,']/',ebforms:enum/@namepath)" />
				</xsl:attribute>
				<xsl:if test="ebforms:content-class!='' or ebforms:content-width!=''">
					<xsl:attribute name="class">
						<xsl:if test="ebforms:content-width!=''">
							<xsl:value-of select="concat('W',ebforms:content-width)" />
							<xsl:if test="ebforms:content-class!=''">
								<xsl:value-of select="' '" />
							</xsl:if>
						</xsl:if>
						<xsl:value-of select="ebforms:content-class" />
					</xsl:attribute>
				</xsl:if>
				<xsl:if test="$displayLabel='true' and $showcaption='true' and ebforms:businessTerm!=''">
					<xsl:element name="xf:label">
						<xsl:if test="ebforms:caption-class!='' or ebforms:caption-width!=''">
							<xsl:attribute name="class">
								<xsl:if test="ebforms:caption-width!=''">
									<xsl:value-of select="concat('W',ebforms:caption-width)" />
									<xsl:if test="ebforms:caption-class!=''">
										<xsl:value-of select="' '" />
									</xsl:if>
								</xsl:if>
								<xsl:value-of select="ebforms:caption-class" />
							</xsl:attribute>
						</xsl:if>
						<xsl:value-of select="ebforms:businessTerm" />
					</xsl:element>
				</xsl:if>
			</xsl:element>
		</xsl:when> 
		
    	<xsl:when test="$element.mode='read'">
			<xsl:element name="xf:output">
				<xsl:attribute name="ref">
					<xsl:value-of select="$ref"/>
				</xsl:attribute>
				<xsl:if test="ebforms:content-class!='' or ebforms:content-width!=''">
					<xsl:attribute name="class">
						<xsl:if test="ebforms:content-width!=''">
							<xsl:value-of select="concat('W',ebforms:content-width)" />
							<xsl:if test="ebforms:content-class!=''">
								<xsl:value-of select="' '" />
							</xsl:if>
						</xsl:if>
						<xsl:value-of select="ebforms:content-class" />
					</xsl:attribute>
				</xsl:if>
				<xsl:if test="$displayLabel='true' and $showcaption='true' and ebforms:businessTerm!=''">
					<xsl:element name="xf:label">
						<xsl:if test="ebforms:caption-class!='' or ebforms:caption-width!=''">
							<xsl:attribute name="class">
								<xsl:if test="ebforms:caption-width!=''">
									<xsl:value-of select="concat('W',ebforms:caption-width)" />
									<xsl:if test="ebforms:caption-class!=''">
										<xsl:value-of select="' '" />
									</xsl:if>
								</xsl:if>
								<xsl:value-of select="ebforms:caption-class" />
							</xsl:attribute>
						</xsl:if>
						<xsl:value-of select="ebforms:businessTerm" />
					</xsl:element>
				</xsl:if>
			</xsl:element>
		</xsl:when> 

 		<xsl:when test="$element.mode='new' and ebforms:enum/@file!=''">
			<xsl:variable name="nodeset">
				<xsl:for-each select="ebforms:enum">
			 		<xsl:variable name="enumfile">
						<xsl:call-template name="get.file.from.path">
							<xsl:with-param name="filepath" select="ebforms:enum/@file" />
						</xsl:call-template>
					</xsl:variable>
				
					<xsl:value-of select="concat('instance(', $quot, substring-before($enumfile,'.'),')',@nodepath) "/>
					<xsl:if test="../ebforms:enumdependency/@attribute!=''" >
						<xsl:value-of select="concat('[',../ebforms:enumdependency/@attribute,'=','instance(',$quot,$doc,'-',$level,$quot,')',../ebforms:enumdependency/@path,']') "/>
					</xsl:if>
					<xsl:value-of select="'|'"/>
				</xsl:for-each>
			</xsl:variable>
			<xsl:element name="xf:select1">
				<xsl:attribute name="ref">
					<xsl:value-of select="$ref" />
				</xsl:attribute>
				<xsl:attribute name="class">
					<xsl:value-of select="'select1'" />
					<xsl:if test="ebforms:content-class!='' or ebforms:content-width!=''">
						<xsl:if test="ebforms:content-width!=''">
							<xsl:value-of select="' '" />
							<xsl:value-of select="concat('W',ebforms:content-width)" />
						</xsl:if>
						<xsl:if test="ebforms:content-class!=''">
							<xsl:value-of select="' '" />
							<xsl:value-of select="ebforms:content-class" />
						</xsl:if>
					</xsl:if>
				</xsl:attribute>
				<xsl:if test="$displayLabel='true' and $showcaption='true'  and ebforms:businessTerm!=''">
					<xsl:element name="xf:label">
						<xsl:if test="ebforms:caption-class!='' or ebforms:caption-width!=''">
							<xsl:attribute name="class">
								<xsl:if test="ebforms:caption-width!=''">
									<xsl:value-of select="concat('W',ebforms:caption-width)" />
										<xsl:if test="ebforms:caption-class!=''">
											<xsl:value-of select="' '" />
										</xsl:if>
								</xsl:if>
								<xsl:value-of select="ebforms:caption-class" />
							</xsl:attribute>
						</xsl:if>
						<xsl:value-of select="ebforms:businessTerm" />
					</xsl:element>
				</xsl:if>
				<xf:itemset nodeset="{substring($nodeset,1,string-length($nodeset)-1)}">
					<xf:label ref="{ebforms:enum/@namepath}" /> 
					<xf:value ref="{ebforms:enum/@valuepath}" />
				</xf:itemset>
				<xf:action ev:event="xforms-value-changed">
					<xf:rebuild model="{$model.id}" /> 
				</xf:action>
			</xsl:element> 
		</xsl:when>
    
		<xsl:when test="$element.mode='new' and ebforms:enumSchema!=''">
			<xsl:variable name="enumschema">
				<xsl:call-template name="get.file.from.path">
					<xsl:with-param name="filepath" select="ebforms:enumSchema" />
				</xsl:call-template>
			</xsl:variable>
		
			<xsl:variable name="nodeset">
				<xsl:value-of select="concat('instance(', $quot, substring-before($enumschema,'.'), $quot,')/xsd:simpleType[@name=', $quot, ebforms:enumSchema/@basetype,$quot,' or @name=substring-after(', $quot, ebforms:enumSchema/@basetype,$quot,',' , $quot, ':' , $quot,')]/xsd:restriction/xsd:enumeration') "/>
			</xsl:variable>
			<xsl:element name="xf:select1">
				<xsl:attribute name="ref">
					<xsl:value-of select="$ref"/>
				</xsl:attribute>
				<xsl:attribute name="class">
					<xsl:value-of select="'select1'" />
					<xsl:if test="ebforms:content-class!='' or ebforms:content-width!=''">
						<xsl:if test="ebforms:content-width!=''">
							<xsl:value-of select="' '" />
							<xsl:value-of select="concat('W',ebforms:content-width)" />
						</xsl:if>
						<xsl:if test="ebforms:content-class!=''">
							<xsl:value-of select="' '" />
							<xsl:value-of select="ebforms:content-class" />
						</xsl:if>
					</xsl:if>
				</xsl:attribute>
				<xsl:if test="$displayLabel='true' and $showcaption='true' and ebforms:businessTerm!=''">
					<xsl:element name="xf:label">
						<xsl:if test="ebforms:caption-class!='' or ebforms:caption-width!=''">
							<xsl:attribute name="class">
								<xsl:if test="ebforms:caption-width!=''">
									<xsl:value-of select="concat('W',ebforms:caption-width)" />
										<xsl:if test="ebforms:caption-class!=''">
											<xsl:value-of select="' '" />
										</xsl:if>
								</xsl:if>
								<xsl:value-of select="ebforms:caption-class" />
							</xsl:attribute>
						</xsl:if>
						<xsl:value-of select="ebforms:businessTerm" />
					</xsl:element>
				</xsl:if>
				<xf:itemset nodeset="{$nodeset}">
					<xf:label ref="xsd:annotation/xsd:documentation/ccts:Name" />
					<xf:value ref="@value" />
				</xf:itemset>
				<xf:action ev:event="xforms-value-changed">
					<xf:rebuild model="{$model.id}" /> 
				</xf:action>
			</xsl:element>
		</xsl:when>

		<xsl:otherwise>
			<xsl:element name="xf:input">
				<xsl:attribute name="ref">
					<xsl:value-of select="$ref"/>
				</xsl:attribute>
				<xsl:if test="ebforms:content-class!='' or ebforms:content-width!=''">
					<xsl:attribute name="class">
						<xsl:if test="ebforms:content-width!=''">
							<xsl:value-of select="concat('W',ebforms:content-width)" />
								<xsl:if test="ebforms:content-class!=''">
									<xsl:value-of select="' '" />
								</xsl:if>
						</xsl:if>
						<xsl:value-of select="ebforms:content-class" />
					</xsl:attribute>
				</xsl:if>
				<xsl:attribute name="incremental">
					<xsl:value-of select="'true'"/>
				</xsl:attribute>
				<xsl:if test="$displayLabel='true' and $showcaption='true' and ebforms:businessTerm!=''">
					<xsl:element name="xf:label">
						<xsl:if test="ebforms:caption-class!='' or ebforms:caption-width!=''">
							<xsl:attribute name="class">
								<xsl:if test="ebforms:caption-width!=''">
									<xsl:value-of select="concat('W',ebforms:caption-width)" />
										<xsl:if test="ebforms:caption-class!=''">
											<xsl:value-of select="' '" />
										</xsl:if>
								</xsl:if>
								<xsl:value-of select="ebforms:caption-class" />
							</xsl:attribute>
						</xsl:if>
						<xsl:value-of select="ebforms:businessTerm" />
					</xsl:element>
				</xsl:if>
				<xf:action ev:event="xforms-value-changed">
					<xf:rebuild model="{$model.id}" /> 
				</xf:action>
			</xsl:element>
		</xsl:otherwise>
	</xsl:choose>

	<xsl:for-each select="ebforms:attribute">
		<xsl:element name="xf:group">
			<xsl:if test="ebforms:class!='' or ebforms:width!=''">
				<xsl:attribute name="class">
					<xsl:if test="ebforms:width!=''">
						<xsl:value-of select="concat('W',ebforms:width)" />
						<xsl:if test="ebforms:class!=''">
							<xsl:value-of select="' '" />
						</xsl:if>
					</xsl:if>
					<xsl:value-of select="ebforms:class" />
				</xsl:attribute>
			</xsl:if>
			<xsl:call-template name="HandleDataType">
				<xsl:with-param name="ref" select="concat($ref,'/@',@name)" />
				<xsl:with-param name="showcaption" select="$showcaption"/>
				<xsl:with-param name="element.mode" select="$element.mode"/>
				<xsl:with-param name="instance.name" select="$instance.name" />
				<xsl:with-param name="instance.level" select="$instance.level" />
			</xsl:call-template>
		</xsl:element>
	</xsl:for-each> 
  </xsl:template>
  <!-- Handle data type -->

 <xsl:template name="FindValuePath">
	<xsl:param name="valuepath" />
	<xsl:param name="searchpath" />
	<xsl:param name="repeat.id" />
	<xsl:choose>
		<xsl:when test="name($searchpath)='ebforms:instance'">
			<xsl:value-of select="$valuepath" />
		</xsl:when>
		<xsl:otherwise>
			<xsl:choose>
				<xsl:when test="$searchpath/@maxOccurs>1 or $searchpath/@maxOccurs='unbounded'">
					<xsl:call-template name="FindValuePath">
						<xsl:with-param name="valuepath" select="concat($searchpath/ebforms:full-name,'[index(',$quot,$repeat.id,$quot,')]/',$valuepath)" />
						<xsl:with-param name="searchpath" select="$searchpath/.." />
						<xsl:with-param name="repeat.id" select="$repeat.id" />
					</xsl:call-template> 
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="FindValuePath">
						<xsl:with-param name="valuepath" select="concat($searchpath/full-name,'/',$valuepath)" />
						<xsl:with-param name="searchpath" select="$searchpath/.." />
						<xsl:with-param name="repeat.id" select="$repeat.id" />
					</xsl:call-template>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:otherwise>
	</xsl:choose> 
</xsl:template> 

 <xsl:template name="setvalue">
	<xsl:param name="targetpath" />	
	<xsl:param name="valuepath" />
	<xsl:param name="instance" /> 
	<xsl:param name="position" />
	<xsl:param name="repeat.name" />
	
	<xsl:for-each select="ebforms:element">
				<xsl:if test="ebforms:setvalue!=''">
					<xsl:choose>
						<xsl:when test="$position='first'">
							<xf:setvalue ref="{concat('instance(',$quot,$global.template.instance.name,'-1',$quot,')/',$targetpath,'[1]/',ebforms:setvalue)}" 
					value="{concat('instance(',$quot,$instance,$quot,')/',$valuepath,'/',ebforms:full-name)}" />
						</xsl:when>
						<xsl:when test="$position='before'">
							<xf:setvalue ref="{concat('instance(',$quot,$global.template.instance.name,'-1',$quot,')/',$targetpath,'[index(',$quot,$repeat.name,$quot,')]/',ebforms:setvalue)}" 
					value="{concat('instance(',$quot,$instance,$quot,')/',$valuepath,'/',ebforms:full-name)}" /> 
						</xsl:when>
						<xsl:when test="$position='after'">
							<xf:setvalue ref="{concat('instance(',$quot,$global.template.instance.name,'-1',$quot,')/',$targetpath,'[index(',$quot,$repeat.name,$quot,')]/',ebforms:setvalue)}" 
					value="{concat('instance(',$quot,$instance,$quot,')/',$valuepath,'/',ebforms:full-name)}" /> 
						</xsl:when>
						<xsl:otherwise>
							<xf:setvalue ref="{concat('instance(',$quot,$global.template.instance.name,'-1',$quot,')/',$targetpath,'[last()]/',ebforms:setvalue)}" 
					value="{concat('instance(',$quot,$instance,$quot,')/',$valuepath,'/',ebforms:full-name)}" /> 
						</xsl:otherwise>
					</xsl:choose>
				</xsl:if>
				<xsl:call-template name="setvalue">
					<xsl:with-param name="targetpath" select="$targetpath" />
					<xsl:with-param name="valuepath"  select="concat($valuepath,'/',ebforms:full-name)"/>
					<xsl:with-param name="instance"  select="$instance"/>
					<xsl:with-param name="position"  select="$position"/>
					<xsl:with-param name="repeat.name"  select="$repeat.name"/>
				</xsl:call-template> 

	</xsl:for-each> 
</xsl:template> 


  <xsl:template name="GenerateSwitchCode">
    <xsl:param name="ref" />
    <xsl:param name="element.attributes" /> 
    <!--    <xsl:param name="element.attributes" as="item()*" required="yes" /> -->
    <xsl:param name="showcaption" />

    <xsl:variable name="case.id" select="generate-id()"></xsl:variable>

    <xf:switch>
      <xf:case id="{concat( $case.id, '_simple' )}">
        <div>
          <div>
            <xf:input ref="{$ref}" incremental="'true'">
              <xsl:if test="$showcaption='true'">
                <xf:label class="caption">
                  <xsl:call-template name="getBusinessTerm">
                    <xsl:with-param name="den" select="ebforms:den" />
                    <xsl:with-param name="annotated.business.term" select="''" />
                  </xsl:call-template>:
                </xf:label>
              </xsl:if>
 <!--             	<xf:action ev:event="xforms-value-changed">
				<xf:rebuild model="{$model.id}" /> 
				</xf:action> -->
            </xf:input>
          </div>
          <div class="element">
            <xf:trigger class="show" appearance="minimal">
				<xf:label><img src="sup.gif" width="10" height="10" alt="Show"/></xf:label>
				<!-- <xf:label>S</xf:label> -->
				<xf:action ev:event="DOMActivate">
					<xf:toggle case="{concat( $case.id, '_detailed' )}" />
				</xf:action>
            </xf:trigger>
          </div>
        </div>
      </xf:case>
      <xf:case id="{concat( $case.id, '_detailed' )}">
        <div>
          <div>
            <xf:input ref="{$ref}" incremental="'true'">
              <xsl:if test="$showcaption='true'">
                <xf:label class="caption">
                  <xsl:call-template name="getBusinessTerm">
                    <xsl:with-param name="den" select="ebforms:den" />
                    <xsl:with-param name="annotated.business.term" select="''" />
                  </xsl:call-template>:
                </xf:label>
              </xsl:if>
 <!--               <xf:action ev:event="xforms-value-changed">
					<xf:rebuild model="{$model.id}" /> 
				</xf:action> -->
            </xf:input>
          </div>
          <div class="element">
            <xf:trigger class="hide" appearance="minimal">
				<xf:label><img src="form_closed.gif" width="20" height="20" alt="Hide"/></xf:label>
					<!-- <xf:label>Hide details</xf:label> -->
				<xf:action ev:event="DOMActivate">
					<xf:toggle case="{concat( $case.id, '_simple' )}" />
				</xf:action>
            </xf:trigger>
          </div>
        </div>
        <div class="Supplementary">
          <xsl:for-each select="msxsl:node-set($element.attributes)/attribute">
            <xsl:call-template name="GenerateAttributeCode">
              <xsl:with-param name="ref" select="$ref" />
              <xsl:with-param name="element.attribute" select="." />
            </xsl:call-template>
          </xsl:for-each>
        </div>
      </xf:case>
    </xf:switch>
  </xsl:template>

  <xsl:template name="GenerateAttributeCode">
    <xsl:param name="ref" />
    <xsl:param name="element.attribute" />

    <!-- The attribute is either a list or a simple type -->
    <xsl:choose>
      <xsl:when test="count(msxsl:node-set( $global.element.types.used)/codelistTypes/attribute
				[@name = $element.attribute/@name and @type = $element.attribute/@type] ) = 1">
        <xf:select1 ref="{concat( concat( $ref, '/@' ), $element.attribute/@name )}" class="select1">
          <xf:label class="caption">
            <xsl:value-of select="$element.attribute/@name"/>:
          </xf:label>
          <xf:itemset>
            <xsl:attribute name="nodeset">
              <xsl:text>instance( '</xsl:text>
              <xsl:value-of	select="concat( 'instance.codes.', $element.attribute/@name ) "/>
              <xsl:value-of select="concat( '.', $element.attribute/@type )" />
              <xsl:text>' )/xsd:simpleType/xsd:restriction/xsd:enumeration</xsl:text>
            </xsl:attribute>
            <xf:label ref="xsd:annotation/xsd:documentation/ccts:Name" class="caption" />
            <xf:value ref="@value" />
          </xf:itemset>
        </xf:select1>
      </xsl:when>
      <xsl:otherwise>
        <xf:input>
          <xsl:attribute name="ref">
            <xsl:value-of select="$ref"/>
            <xsl:text>/@</xsl:text>
            <xsl:value-of select="$element.attribute/@name" />
          </xsl:attribute>
          <xsl:attribute name="incremental">
            <xsl:value-of select="'true'"/>
          </xsl:attribute>
          <xf:label class="caption">
            <xsl:value-of 
						select="concat( $element.attribute/@name, ': ' )" />
          </xf:label>
        </xf:input>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- 
		Returns the den of a BIE element, of the type and of all its children (recursively)
		Context is the element in a sequence
		@parent 		- 	The parent element from which to get the children.
		@parent.schema 	- 	The schema in which the element is declared. 
	-->
  <xsl:template name="GetChildDens">
    <xsl:param name="parent" />
    <xsl:param name="parent.schema.name" />
		<xsl:copy-of select="xsd:annotation/xsd:documentation/ccts:DictionaryEntryName" />
	    <xsl:copy-of select="msxsl:node-set(document($parent.schema.name))
			//xsd:complexType[@name = substring-after($parent,':')]/xsd:annotation/xsd:documentation/ccts:DictionaryEntryName" />
		<xsl:for-each select="msxsl:node-set(document($parent.schema.name))//xsd:complexType[@name = substring-after($parent,':')]/xsd:sequence/xsd:element">
			<xsl:variable name="schema.name" >
				 <xsl:call-template name="GetDataTypeDocument" >
					<xsl:with-param name="element.type" select="@type" />
					<xsl:with-param name="element.schema.name" select="$parent.schema.name" />
				</xsl:call-template>
			</xsl:variable>
			<xsl:call-template name="GetChildDens">
				<xsl:with-param name="parent" select="@type" />
				<xsl:with-param name="parent.schema.name" select="$schema.name" />
			</xsl:call-template>
      </xsl:for-each>
  </xsl:template>
  
  <!-- 
		Returns all child elements for a parent, their acronym, den and their sequence if it is defined in the annotations file.
		If the sequence was not defined it is set to 99999 so that the child comes after the the elements that have
		a sequence defined in the annotations file.
		@parent 		- 	The parent element from which to get the children.
		@parent.schema 	- 	The schema in which the element is declared. 
	-->
  
 <!-- <xsl:template name="GetChildElements">
    <xsl:param name="parent" />
    <xsl:param name="parent.schema.name" />
      <xsl:for-each select="msxsl:node-set(document($parent.schema.name))//xsd:complexType[@name = substring-after($parent,':')]/xsd:sequence/xsd:element">
       <xsl:variable name="schema.node.set" select="." />
        <xsl:call-template name="FillElementType">
			<xsl:with-param name="parent.schema.name" select="$parent.schema.name" />
			<xsl:with-param name="schema.node.set" select="$schema.node.set" />
		</xsl:call-template>
      </xsl:for-each>
  </xsl:template> -->
  <!-- This template assembles various element attributes in a nodeset -->
  <!-- Context is "element" in schema -->
<xsl:template name="FillElementType">
    <xsl:param name="parent.schema.name" />
    <xsl:param name="schema.node.set" /> 

	

</xsl:template>

  <!--
		Returns the document in which the datatype is defined.
		@element.type - The element type for which the schema is looked up.
		@element.schema- The document in which the datatype is used (not defined).
		
		Note I: This template assumes that the document in which
		the dataType is used also imports the document where the datatype is declared.
		
		Note II: The dataType variable must contain the namespace prefix of the namespace in 
		which the datatype is declared.
	-->
  <xsl:template name="GetDataTypeDocument" >
    <xsl:param name="element.type" />
    <xsl:param name="element.schema.name" />

    <xsl:variable name="element.type.prefix" select="substring-before( $element.type, ':' )" />
  
    <xsl:variable name="element.type.ns">
      <xsl:choose>
        <xsl:when test="$element.type.prefix = ''">
          <xsl:value-of select="document($element.schema.name)/xsd:schema/@targetNamespace"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="document($element.schema.name)/xsd:schema/namespace::*[name() = $element.type.prefix ]" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    
     <xsl:variable name="child.schema.name" select="document($element.schema.name)/xsd:schema/xsd:import[@namespace=$element.type.ns]/@schemaLocation" /> 
    
    <xsl:choose>
     <xsl:when test="$element.type.ns!=document($element.schema.name)/xsd:schema/@targetNamespace">
		 <xsl:choose>
			<xsl:when test="document(document($element.schema.name)/xsd:schema/xsd:import[@namespace=$element.type.ns]/@schemaLocation)//xsd:complexType[@name=$element.type or @name=substring-after($element.type,':')]/@name!=''">
				<xsl:value-of select="document($element.schema.name)/xsd:schema/xsd:import[@namespace=$element.type.ns]/@schemaLocation" /> 
			</xsl:when>
			<xsl:when test="document(document($element.schema.name)/xsd:schema/xsd:import[@namespace=$element.type.ns]/@schemaLocation)//xsd:simpleType[@name=$element.type or @name=substring-after($element.type,':')]/@name!=''">
				<xsl:value-of select="document($element.schema.name)/xsd:schema/xsd:import[@namespace=$element.type.ns]/@schemaLocation" /> 
			</xsl:when>
			<xsl:otherwise>
			<xsl:value-of select="document(document($element.schema.name)/xsd:schema/xsd:import[@namespace=$element.type.ns]/@schemaLocation)//xsd:schema/xsd:include/@schemaLocation" />
				</xsl:otherwise>
		</xsl:choose>
      </xsl:when>
      <xsl:when test="document($element.schema.name)//xsd:complexType[@name=$element.type or @name=substring-after($element.type,':')]/@name!=''">
           <xsl:value-of select="$element.schema.name" />
	 </xsl:when>
      <xsl:otherwise>
		<xsl:value-of select="document($element.schema.name)//xsd:schema/xsd:include/@schemaLocation" /> 
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- 
		This template returns the default value for a CoreComponent with given dictionary entry name.
	-->
  
<xsl:template name="GetDerivedValue">
    <xsl:param name="element.den" />
    <xsl:param name="parent.den" />
    <xsl:param name="dercount" />
    
		<xsl:variable name="derivation.path">
			<xsl:call-template name="GetDerivationPath">
				<xsl:with-param name="path" select="''" />
				<xsl:with-param name="denstring"  select="concat($element.den,'\',$parent.den)" />
			</xsl:call-template>
		</xsl:variable>
	
<!--		 <xsl:value-of select="$parent.den" /> -->
	<xsl:variable name="path">
	 <xsl:call-template name="MakePath">
		<xsl:with-param name="path" select="$derivation.path" />
		<xsl:with-param name="node" select="$global.template.instance.node/*[name()=$global.template.instance.name]" />
	</xsl:call-template> 
  </xsl:variable>
  
	<xsl:value-of select="msxsl:node-set($path)/*[$dercount]" />   
  
</xsl:template>

<xsl:template name="GetDerivationPath">
	<xsl:param name="path" />
	<xsl:param name="denstring" />
				
	<xsl:variable name="thisden">		
		<xsl:choose>
			<xsl:when test="contains($denstring,'\')">
				<xsl:value-of select="substring-before($denstring,'\')" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$denstring" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>		
			

	<xsl:variable name="thispath">
		<xsl:call-template name="FindPropertyValue">
			<xsl:with-param name="den.current" select="$thisden" />
			<xsl:with-param name="den.start"  select="$thisden" />
			<xsl:with-param name="property" select="'derived'" />
			<xsl:with-param name="class" select="'content'" />
			<xsl:with-param name="value" select="'value'"/>
			<xsl:with-param name="mode" select="'new'"/>
			
		</xsl:call-template>
	</xsl:variable>
	
	
	<xsl:choose>
		<xsl:when test="contains($denstring,'\') and $path!='' and $thispath!=''">
			<xsl:call-template name="GetDerivationPath">
				<xsl:with-param name="path" select="concat($thispath,'/',$path)" />
				<xsl:with-param name="denstring" select="substring-after($denstring,'\')" />
			</xsl:call-template>
		</xsl:when>
		<xsl:when test="contains($denstring,'\') and $thispath!=''"> 
			<xsl:call-template name="GetDerivationPath">
				<xsl:with-param name="path" select="$thispath" />
				<xsl:with-param name="denstring" select="substring-after($denstring,'\')" />
			</xsl:call-template>
		</xsl:when>
		<xsl:when test="contains($denstring,'\')">
			<xsl:call-template name="GetDerivationPath">
				<xsl:with-param name="path" select="''" />
				<xsl:with-param name="denstring" select="substring-after($denstring,'\')" />
			</xsl:call-template>
		</xsl:when>
		<xsl:when test="$path!='' and $thispath!=''">
			<xsl:value-of select="concat($thispath,'/',$path)" />
		</xsl:when>
		<xsl:when test="$thispath!=''">
			<xsl:value-of select="$thispath" />
		</xsl:when> 
		<xsl:when test="$path!=''">
			<xsl:value-of select="$path" />
		</xsl:when> 
	</xsl:choose> 

</xsl:template>

<xsl:template name="MakePath">
<xsl:param name="path" />
<xsl:param name="node" />
	<xsl:choose>
		<xsl:when test="contains($path,'/')">
			<xsl:call-template name="MakePath">
				<xsl:with-param name="path" select="substring-after($path,'/')" />
				<xsl:with-param name="node" 
				select="$node//*[name()=substring-before(substring-before($path,'/'),'[')][position()=substring-before(substring-after(substring-before($path,'/'),'['),']')]" />
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<xsl:copy-of select="$node/*[name()=$path][position()=substring-before(substring-after($path,'['),']')]" /> 
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

  
<!--  <xsl:template name="GetPath">
    <xsl:param name="path" />
    <xsl:param name="startpath" />
    <xsl:choose>
      <xsl:when test="contains($startpath,'/')">
        <xsl:call-template name="GetPath">
          <xsl:with-param name="path" select="concat($path,substring-before($startpath,'/'),'/')" />
          <xsl:with-param name="startpath" select="substring-after($startpath,'/')" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$path" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template> -->
  <!-- 
		Returns the name of the type on which element.type is based.
	-->
  <xsl:template name="GetBaseType" >
    <xsl:param name="element.type" />
    <xsl:param name="element.type.schema" />

    <xsl:variable name="type" select="substring-after( $element.type, ':' )" />

    <xsl:variable name="element.type.def">
      <!--      <xsl:choose>
        <xsl:when test="document( $element.type.schema )/xsd:schema/xsd:complexType[@name = $type] != ''">
          <xsl:value-of select="document( $element.type.schema )/xsd:schema/xsd:complexType[@name = $type]"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="document( $element.type.schema )/xsd:schema/xsd:simpleType[@name = $type]" />
        </xsl:otherwise>
      </xsl:choose>
      <xsl:choose> -->
      <xsl:value-of select="document( $element.type.schema )/xsd:schema/xsd:complexType[@name = $type]
          | document($element.type.schema )/xsd:schema/xsd:simpleType[@name = $type]" />
    </xsl:variable>
    <!--		<xsl:variable name="element.type.def" 
			select="if( exists( document( $element.type.schema )/xsd:schema/xsd:complexType[@name = $type] ) )
				then document( $element.type.schema )/xsd:schema/xsd:complexType[@name = $type]
				else document( $element.type.schema )/xsd:schema/xsd:simpleType[@name = $type]" /> -->
    <xsl:value-of select="msxsl:node-set($element.type.def)/xsd:restriction/@base | msxsl:node-set($element.type.def)/xsd:simpleContent/xsd:restriction/@base" />
    <!--    <xsl:choose>
      <xsl:when test="exists( $element.type.def/xsd:restriction/@base )">
        <xsl:value-of select="$element.type.def/xsd:restriction/@base"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$element.type.def/xsd:simpleContent/xsd:restriction/@base" />
      </xsl:otherwise>
    </xsl:choose> -->

    <!--   <xsl:value-of select="if( exists( $element.type.def/xsd:restriction/@base ) )
			then $element.type.def/xsd:restriction/@base
			else $element.type.def/xsd:simpleContent/xsd:restriction/@base" /> -->
  </xsl:template>

  <!-- 
		Looks for the unqualfied datatype on which this qualified datatype is based.
		
		@element.type - The type for which to look up on which Unqualified Data Type it's based.
		@element.type.schema - The document in which the element.type is defined
	-->
  <xsl:template name="GetUnqualifiedDataType" >
    <xsl:param name="element.type" />
    <xsl:param name="element.type.schema" />

    <xsl:variable name="element.type.base" >
      <xsl:call-template name="GetBaseType">
        <xsl:with-param name="element.type" select="$element.type" />
        <xsl:with-param name="element.type.schema" select="$element.type.schema" />
      </xsl:call-template>
    </xsl:variable>

    <xsl:choose>
      <!--
				According to the spec each unqualified datatype should be in a namespace 
				with prefix udt. See section 7.6.3 of the XML NDS.
				
				First case:  element.type is itself an unqualified data type.
			-->
      <xsl:when test="substring-before( $element.type, ':' ) = 'udt' ">
        <xsl:value-of select="$element.type" />
      </xsl:when>
      <!-- 
				Second case: element.type is a qualified datatype. This can either be:
				- based on another qualified datatype
				- based on an unqualified datatype
				- or it's an simpletype
			-->
      <xsl:when test="substring-before( $element.type, ':' ) = 'qdt' ">
        <xsl:choose>
          <xsl:when test="substring-before( $element.type.base, ':' ) = 'qdt'">
            <xsl:variable name="element.basetype.schema">
              <xsl:call-template name="GetDataTypeDocument">
                <xsl:with-param name="element.type" select="$element.type.base" />
                <xsl:with-param name="element.schema.name" select="$element.type.schema" />
              </xsl:call-template>
            </xsl:variable>
            <xsl:call-template name="GetUnqualifiedDataType">
              <xsl:with-param name="element.type" select="$element.type.base"/>
              <xsl:with-param name="element.type.schema" select="$element.basetype.schema"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:when test="substring-before( $element.type.base, ':' ) = 'udt'">
            <xsl:value-of select="$element.type.base" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:variable name="element.den" 
							select="document( $element.type.schema )/xsd:schema
							/xsd:simpleType[@name = substring-after( $element.type, ':' )]
							/xsd:annotation/xsd:documentation/ccts:DictionaryEntryName" />

            <xsl:choose>
              <xsl:when test="contains( $element.den, 'Code. Type' )">
                <xsl:text>udt:CodeType</xsl:text>
              </xsl:when>
              <xsl:when test="contains( $element.den, 'Identifier. Type' )">
                <xsl:text>udt:IDType</xsl:text>
              </xsl:when>
              <xsl:otherwise>
                <!-- 
                <xsl:value-of select="error( 
									QName( 'http://code.google.com/p/eb-forms/functions', 'GetUnqualifiedDataType' )
									, concat( 'GetUnqualifiedDataType: QDT has invalid den, should contain Identifier. Type or Code. Type. element.den: '
									, $element.den )  ) " 
								/> -->
              </xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <!-- <xsl:value-of select="error( 
					QName( 'http://code.google.com/p/eb-forms/functions', 'GetUnqualifiedDataType' )
					, concat( 'GetUnqualifiedDataType: element.type is not an UDT or a QDT '
					, $element.type )  ) " 
				/> -->
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="BasedOnSimpleType">
    <xsl:param name="element.type" />
    <xsl:param name="element.type.schema" />

    <xsl:variable name="element.type.base" >
      <xsl:call-template name="GetBaseType">
        <xsl:with-param name="element.type" select="$element.type" />
        <xsl:with-param name="element.type.schema" select="$element.type.schema" />
      </xsl:call-template>
    </xsl:variable>

    <xsl:choose>
      <!--	First case:  element.type is itself an unqualified data type. -->
      <xsl:when test="substring-before( $element.type, ':' ) = 'udt'">
        <xsl:value-of select="false()" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="substring-before( $element.type.base, ':' ) = 'qdt'">
            <xsl:variable name="element.basetype.schema">
              <xsl:call-template name="GetDataTypeDocument">
                <xsl:with-param name="element.type" select="$element.type.base" />
                <xsl:with-param name="element.schema.name" select="$element.type.schema" />
              </xsl:call-template>
            </xsl:variable>
            <xsl:call-template name="BasedOnSimpleType">
              <xsl:with-param name="element.type" select="$element.type.base"/>
              <xsl:with-param name="element.type.schema" select="$element.basetype.schema"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:when test="substring-before( $element.type.base, ':' ) = 'udt'">
            <xsl:value-of select="false()" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="true()" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="GetUnusedAttributes" >
    <xsl:param name="element.den" />

    <xsl:copy-of select="$global.annotations.node/ebforms:annotations/ebforms:core-component
			[@den = $element.den]/ebforms:unused-attributes/ebforms:attribute" />
  </xsl:template>

  <xsl:template name="GetUsedAttributes" >
    <xsl:param name="element.den" />
    <xsl:param name="element.type" />
    <xsl:param name="element.type.schema" />

    <xsl:variable name="element.type.isRestrictedSimpleType">
      <xsl:call-template name="BasedOnSimpleType">
        <xsl:with-param name="element.type" select="$element.type" />
        <xsl:with-param name="element.type.schema" select="$element.type.schema" />
      </xsl:call-template>
    </xsl:variable>

    <!-- 
			If element.type.isRestrictedSimpleType is true then whe have a qdt that is a restricted
			xsl:simpleType. This means that it for sure has no attributes. Otherwhise whe have to
			look up the attributes.
		-->
    <xsl:if test="$element.type.isRestrictedSimpleType = 'false'">
      <xsl:variable name="element.type.attributes">
 <!--       <xsl:call-template name="GetAttributeTypes" >
          <xsl:with-param name="type" select="$element.type" />
          <xsl:with-param name="type.schema" select="$element.type.schema" />
        </xsl:call-template> -->
      </xsl:variable>

      <xsl:variable name="element.type.attributes.unused">
        <xsl:call-template name="GetUnusedAttributes" >
          <xsl:with-param name="element.den" select="xsd:annotation/xsd:documentation/ccts:DictionaryEntryName" />
        </xsl:call-template>
      </xsl:variable>

      <xsl:for-each select="msxsl:node-set($element.type.attributes)/attribute">
        <xsl:variable name="name" select="@name" />
        <xsl:variable name="type" select="@type" />
        <xsl:if test="count( msxsl:node-set($element.type.attributes.unused)/attribute[@name = $name] ) = 0">
          <xsl:element name="attribute">
            <xsl:attribute name="name">
              <xsl:value-of select="$name" />
            </xsl:attribute>

            <xsl:attribute name="type">
              <xsl:value-of select="$type" />
            </xsl:attribute> 
          </xsl:element>
        </xsl:if>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>

  <xsl:template name="GetAttributeInfo">
    <xsl:param name="type" />
    <xsl:param name="type.schema" />
    <xsl:param name="type.attribute" />

    <xsl:variable name="attribute.type" 
			select="document( $type.schema )/xsd:schema/xsd:complexType
			[@name = substring-after( $type, ':' )]/xsd:simpleContent/xsd:extension/xsd:attribute
			[@name = $type.attribute]/@type" />

    <xsl:choose>
      <!-- 
				If the attribute type is empty and whe have an unqualified datatype then return
				nothing
				If the attribute type is empty and whe have an qualified datatype then we have
				to look up the basetype of this datatype and check if the type of the attribute is
				declared there.
			-->
      <xsl:when test="not( $attribute.type ) and not( substring-before( $type, ':' ) = 'udt' )">
        <xsl:variable name="type.base" 
					select="document( $type.schema )/xsd:schema/xsd:complexType
					[@name = substring-after( $type, ':' )]/xsd:simpleContent/xsd:restriction/@base" />

        <xsl:variable name="type.base.schema">
          <xsl:call-template name="GetDataTypeDocument">
            <xsl:with-param name="element.type" select="$type.base"/>
            <xsl:with-param name="element.schema.name" select="$type.schema"/>
          </xsl:call-template>
        </xsl:variable>

        <xsl:call-template name="GetAttributeInfo">
          <xsl:with-param name="type" select="$type.base"/>
          <xsl:with-param name="type.schema"  select="$type.base.schema"/>
          <xsl:with-param name="type.attribute" select="$type.attribute" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="attribute.type.schema">
          <xsl:call-template name="GetDataTypeDocument">
            <xsl:with-param name="element.type" select="$attribute.type" />
            <xsl:with-param name="element.schema.name" select="$type.schema" />
          </xsl:call-template>
        </xsl:variable>

        <xsl:element name="attribute">
          <xsl:attribute name="name">
            <xsl:value-of select="$type.attribute" />
          </xsl:attribute>
          <xsl:attribute name="type">
            <xsl:value-of select="$attribute.type" />
          </xsl:attribute>
          <xsl:attribute name="type.schema">
            <xsl:value-of select="$attribute.type.schema" />
          </xsl:attribute>
        </xsl:element>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="FindPropertyValue">
    <xsl:param name="den.current" />
    <xsl:param name="den.start" />
    <xsl:param name="property" />
    <xsl:param name="class" />
    <xsl:param name="value" />
    <xsl:param name="mode" />

    <xsl:variable name="property.value" >
		<xsl:choose>
			<xsl:when test="$global.annotations.node/ebforms:annotations/ebforms:core-component[
				@den=$den.current and ($mode='' or @mode=$mode)]/ebforms:element[@class=$class]/descendant::*[name()=$property]/@*[name()=$value]!=''" >
				 <xsl:value-of select="$global.annotations.node/ebforms:annotations/ebforms:core-component[
					@den=$den.current and ($mode='' or @mode=$mode)]/ebforms:element[@class=$class]/descendant::*[name()=$property]/@*[name()=$value]" />
			</xsl:when>
			<xsl:when test="$global.annotations.node/ebforms:annotations/ebforms:core-component[
				@den=$den.current and not(@mode)]/ebforms:element[@class=$class]/descendant::*[name()=$property]/@*[name()=$value]!=''" >
				 <xsl:value-of select="$global.annotations.node/ebforms:annotations/ebforms:core-component[
					@den=$den.current and not(@mode)]/ebforms:element[@class=$class]/descendant::*[name()=$property]/@*[name()=$value]" /> 
			</xsl:when>
			<xsl:otherwise>
				 <xsl:value-of select="''" />
			</xsl:otherwise>
		</xsl:choose>
    </xsl:variable>

    <xsl:choose>
	  <xsl:when test="$property.value =''">
		<xsl:choose>
            
		  <xsl:when test="contains( $den.current, 'BBIE' )">
          </xsl:when>
  
          <xsl:when test="contains( $den.current, 'ABIE' )">
          </xsl:when>

           <xsl:when test="not( contains( $den.current, '. ' ) )
			   and contains( $den.current,'_ ' ) ">
            <xsl:call-template name="FindPropertyValue">
              <xsl:with-param name="den.current" 
								select="substring-after( $den.current,'_ ' )" />
              <xsl:with-param name="den.start" select="$den.start"/>
              <xsl:with-param name="property" select="$property"/>
              <xsl:with-param name="class" select="$class"/>
               <xsl:with-param name="value" select="$value"/>
                <xsl:with-param name="mode" select="$mode"/>
          </xsl:call-template>
          </xsl:when>

         <xsl:when test="not( contains( $den.current, '. Type' ) )
						and not( contains( $den.current, '. Details' ) )
						and contains( substring-before( $den.current,'. ' ), '_ ' ) ">
            <xsl:call-template name="FindPropertyValue">
              <xsl:with-param name="den.current" 
								select="substring-after( $den.current,'_ ' )" />
              <xsl:with-param name="den.start" select="$den.start"/>
              <xsl:with-param name="property" select="$property"/>
              <xsl:with-param name="class" select="$class"/>
              <xsl:with-param name="value" select="$value"/>
              <xsl:with-param name="mode" select="$mode"/>
          </xsl:call-template>
          </xsl:when> 

          <xsl:when test="not( contains( $den.current,'. Type' ) ) 
						and not( contains( $den.current, '. Details' ) )
						and contains( substring-before( substring-after( $den.current, '. ' ), '. ' ),'_ ' )">
            <xsl:call-template name="FindPropertyValue">
              <xsl:with-param name="den.current" 
								select="concat( substring-before( $den.start,'. ' ),'. ',
								substring-after( substring-after( $den.current,'. ' ), '_ ' ) )"/>
              <xsl:with-param name="den.start" select="$den.start" />
              <xsl:with-param name="property" select="$property"/>
              <xsl:with-param name="class" select="$class"/>
               <xsl:with-param name="value" select="$value"/>
                 <xsl:with-param name="mode" select="$mode"/>
           </xsl:call-template>
          </xsl:when>
 
           <xsl:when test="not( contains($den.current,'. Type' ) ) 
						and not( contains( $den.current, '. Details' ) )
						and contains( substring-after(substring-after($den.current, '. ' ), '. ' ),'_ ' )">
            <xsl:call-template name="FindPropertyValue">
              <xsl:with-param name="den.current" 
								select="concat( substring-before($den.start,'. ' ),'. ',
								substring-before( substring-after($den.start,'. ' ), '. ' ),'. ',
								substring-after(substring-after(substring-after($den.current,'. '),'. '),'_ ') )"/>
              <xsl:with-param name="den.start" select="$den.start" />
              <xsl:with-param name="property" select="$property"/>
              <xsl:with-param name="class" select="$class"/>
              <xsl:with-param name="value" select="$value"/>
                 <xsl:with-param name="mode" select="$mode"/>
          </xsl:call-template>
          </xsl:when>
          
          <xsl:when test="not( contains( $den.current, '. Type' ) )
						and not( contains( $den.current, '. Details' ) )
						and (contains(substring-after(substring-after($den.current,'. '),'. '),'Text') or
								contains(substring-after(substring-after($den.current,'. '),'. '),'Identifier') or
								contains(substring-after(substring-after($den.current,'. '),'. '),'Code') or
								contains(substring-after(substring-after($den.current,'. '),'. '),'Date') or
								contains(substring-after(substring-after($den.current,'. '),'. '),'Time') or
								contains(substring-after(substring-after($den.current,'. '),'. '),'Date Time') or
								contains(substring-after(substring-after($den.current,'. '),'. '),'Indicator') or
								contains(substring-after(substring-after($den.current,'. '),'. '),'Measure') or
								contains(substring-after(substring-after($den.current,'. '),'. '),'Quantity') or
								contains(substring-after(substring-after($den.current,'. '),'. '),'Amount') or
								contains(substring-after(substring-after($den.current,'. '),'. '),'Numeric'))">
            <xsl:call-template name="FindPropertyValue">
              <xsl:with-param name="den.current"
								select="concat( substring-after( substring-after( $den.current, '. ' )
								, '. ' ), '. Type' )"/>
              <xsl:with-param name="den.start" select="$den.start"/>
              <xsl:with-param name="property" select="$property"/>
              <xsl:with-param name="class" select="$class"/>
              <xsl:with-param name="value" select="$value"/>
                 <xsl:with-param name="mode" select="$mode"/>
         </xsl:call-template>
          </xsl:when>

          <xsl:when test="not( contains( $den.current, '. Type' ) )
						and not( contains( $den.current, '. Details' ) )">
            <xsl:call-template name="FindPropertyValue">
              <xsl:with-param name="den.current"
								select="concat( substring-after( substring-after( $den.current, '. ' )
								, '. ' ), '. Details' )"/>
              <xsl:with-param name="den.start" select="$den.start"/>
              <xsl:with-param name="property" select="$property"/>
              <xsl:with-param name="class" select="$class"/>
              <xsl:with-param name="value" select="$value"/>
                 <xsl:with-param name="mode" select="$mode"/>
          </xsl:call-template>
          </xsl:when>
          
          <xsl:when test="contains( $den.current,'. Details' ) and contains($den.current,'_ ')">
            <xsl:call-template name="FindPropertyValue">
              <xsl:with-param name="den.current" 
								select="substring-after( $den.current, '_ ' )"/>
              <xsl:with-param name="den.start" select="$den.start"/>
              <xsl:with-param name="property" select="$property"/>
              <xsl:with-param name="class" select="$class"/>
              <xsl:with-param name="value" select="$value"/>
                  <xsl:with-param name="mode" select="$mode"/>
        </xsl:call-template>
          </xsl:when>

          <xsl:when test="contains( $den.current, '. Type' ) and not(contains(substring-after($den.current,'. '),'. ')) and 
						contains(substring-after($den.start,'. '),'. ') and contains( $den.current, '_ ' )">
            <xsl:call-template name="FindPropertyValue">
              <xsl:with-param name="den.current" 
								select="concat( substring-before( $den.start,'. ')
								, '. ', substring-before( substring-after( $den.start, '. ' ), '. ' )
								, '. ', substring-after( substring-before( $den.current, '. ' ), '_ ' ) )"/>
              <xsl:with-param name="den.start" select="$den.start"/>
              <xsl:with-param name="property" select="$property"/>
              <xsl:with-param name="class" select="$class"/>
              <xsl:with-param name="value" select="$value"/>
                 <xsl:with-param name="mode" select="$mode"/>
          </xsl:call-template>
          </xsl:when>

          <xsl:when test="contains( $den.current, '. Type' ) and contains( $den.current, '_ ' )">
            <xsl:call-template name="FindPropertyValue">
              <xsl:with-param name="den.current" 
								select="concat( substring-after( substring-before( $den.current,'. ' ), '_ ' ), '. Type')"/>
              <xsl:with-param name="den.start" select="$den.start"/>
              <xsl:with-param name="property" select="$property"/>
              <xsl:with-param name="class" select="$class"/>
              <xsl:with-param name="value" select="$value"/>
                  <xsl:with-param name="mode" select="$mode"/>
          </xsl:call-template>
          </xsl:when>
 
            <xsl:when test="contains( $den.current, '. Type' )">
            <xsl:call-template name="FindPropertyValue">
              <xsl:with-param name="den.current" select="'BBIE'"/>
              <xsl:with-param name="den.start" select="$den.start"/>
              <xsl:with-param name="property" select="$property"/>
              <xsl:with-param name="class" select="$class"/>
              <xsl:with-param name="value" select="$value"/>
                  <xsl:with-param name="mode" select="$mode"/>
           </xsl:call-template>
          </xsl:when>
  
          <xsl:when test="contains( $den.current, '. Details' )">
            <xsl:call-template name="FindPropertyValue">
              <xsl:with-param name="den.current" select="'ABIE'"/>
              <xsl:with-param name="den.start" select="$den.start"/>
              <xsl:with-param name="property" select="$property"/>
              <xsl:with-param name="class" select="$class"/>
              <xsl:with-param name="value" select="$value"/>
                    <xsl:with-param name="mode" select="$mode"/>
         </xsl:call-template>
          </xsl:when>

          <xsl:otherwise>
            <!-- At this point we have an unqualified datatype or an unqualified ABIE -->
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$property.value" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

 <!-- get a term for the caption. Context is node in instance -->
  <xsl:template name="getBusinessTerm" >
    <xsl:param name="den" />
    <xsl:param name="annotated.business.term" />

    <xsl:variable name="caption">
      <xsl:call-template name="FindPropertyValue">
        <xsl:with-param name="den.current" select="$den"/>
        <xsl:with-param name="den.start" select="$den"/>
        <xsl:with-param name="property" select="'text'"/>
        <xsl:with-param name="class" select="'caption'"/>
        <xsl:with-param name="value" select="'value'"/>
        <xsl:with-param name="mode" select="''"/>
     </xsl:call-template>
    </xsl:variable>

    <xsl:choose>
      <!-- <xsl:when test="$global.annotations.node//ebforms:core-component[@den=$den]/ebforms:element[@class='caption']/ebforms:show/@value='false'" >
        <xsl:value-of select="''" />
      </xsl:when> -->
      <xsl:when test="$caption!=''">
        <xsl:value-of select="$caption" />
      </xsl:when>
      
      <xsl:when test="$annotated.business.term!=''" >
         <xsl:value-of select="$annotated.business.term" />
      </xsl:when>
      
<!--      <xsl:when test="xsd:annotation/xsd:documentation/ccts:BusinessTerm" >
         <xsl:value-of select="xsd:annotation/xsd:documentation/ccts:BusinessTerm" />
      </xsl:when> -->
	  <xsl:when test="$den!=''">
		<xsl:choose>
			<xsl:when test="substring-after($den,'. ')!=''">
				<xsl:choose>
					<xsl:when test="substring-before(substring-after($den,'. '),'. ')!=''">
						<xsl:call-template name="CamelCase2Words">
							<xsl:with-param name="input" select="translate(substring-before(substring-after($den,'. '),'. '),'_ ',' ')" />
							<xsl:with-param name="sep" select="' '" />
						</xsl:call-template>
					</xsl:when>
					<xsl:otherwise>
						<xsl:call-template name="CamelCase2Words">
							<xsl:with-param name="input" select="translate(substring-before($den,'. '),'_ ',' ')" />
							<xsl:with-param name="sep" select="' '" />
						</xsl:call-template>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="CamelCase2Words">
					<xsl:with-param name="input" select="translate($den,'_ ',' ')" />
					<xsl:with-param name="sep" select="' '" />
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:when>
      
      <!-- search for more generic schema annotations? -->
      <xsl:otherwise>
			<xsl:call-template name="CamelCase2Words">
				<xsl:with-param name="input" select="local-name(.)" />
				<xsl:with-param name="sep" select="' '" />
			</xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="BuildCodeTypelist">
	<xsl:param name="doc"/> 

<!-- code typelist splitsen in nieuwe template -->  
	  <xsl:variable name="doc.types">
		  <xsl:for-each select="msxsl:node-set($global.element.types.used)/ebforms:instance[@name=$doc]/ebforms:element[ebforms:Acronym='BBIE']" >
			  <xsl:element name="type">
				  <xsl:attribute name="name">
					  <xsl:value-of select="@type"/>
				  </xsl:attribute> 
				  <xsl:attribute name="schema">
					  <xsl:value-of select="ebforms:schema"/>
				  </xsl:attribute> 
			  </xsl:element>
		</xsl:for-each>
	  </xsl:variable>
  
  
    <xsl:variable name="codelistTypes">
      <xsl:call-template name="GetCodelistTypes">
         <xsl:with-param name="types.used" select="$doc.types/type[not( @name = preceding-sibling::type/@name)]" /> 
      </xsl:call-template>
    </xsl:variable>

    <!-- Filter out all double types and codelistTypes -->
    <xsl:copy-of select="msxsl:node-set($doc.types)/type[not( @name = preceding-sibling::type/@name)]" />
    <!--    <xsl:copy-of select="$types/type[not( @name = preceding-sibling::type/@name)]" /> -->
    <xsl:element name="codelistTypes">
      <xsl:copy-of select="msxsl:node-set($codelistTypes)/count" />
      <!--      <xsl:copy-of select="$codelistTypes/count" /> -->
      <!--      <xsl:copy-of 
				select="$codelistTypes/attribute[not( @name = preceding-sibling::attribute/@name
				and @type = preceding-sibling::attribute/@type)]" /> -->
      <xsl:copy-of 
				select="msxsl:node-set($codelistTypes)/attribute[not( @name = preceding-sibling::attribute/@name
				and @type = preceding-sibling::attribute/@type)]" />
    </xsl:element>
  </xsl:template>


<!-- Recursively walk through the instance to collect all relevant attributes of the form elements -->
<xsl:template name="GetElementType">
    <xsl:param name="parent.type" />
    <xsl:param name="parent.schema.name" />
    <xsl:param name="parent.schema.node" />
    <xsl:param name="parent.instance.node" />
    <xsl:param name="parent.name" />
    <xsl:param name="parent.derivation.path" />
    <xsl:param name="level" />
    <xsl:param name="dercount" />
    <xsl:param name="parent.mode" />
    <xsl:param name="parent.show" />

	<xsl:variable name="element.name">
		<xsl:choose>
			<xsl:when test="contains(name(.),':')">
				<xsl:value-of select="substring-after(name(.),':')" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="name(.)" />
			</xsl:otherwise>
		</xsl:choose> 
	</xsl:variable> 

	<xsl:variable name="element.prefix" select="substring-before(name(.),':')" /> 

	<xsl:variable name="element.namespace">
		<xsl:choose>
			<xsl:when test="$element.prefix!=''">
				<xsl:value-of select="//namespace::*[name() = $element.prefix]" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="document($parent.schema.name)//xsd:schema/@targetNamespace"/>
			</xsl:otherwise>
		</xsl:choose> 
	</xsl:variable>
	
	<!-- Get the name of the schema in which the element is declared: either the schema of the parent, an included schema or an imported schema -->	
	<xsl:variable name="element.schema.name.collect">
		<xsl:choose>
			<xsl:when test="$element.namespace=document($parent.schema.name)//xsd:schema/@targetNamespace">
				<xsl:choose>
					<xsl:when test="document($parent.schema.name)//xsd:complexType[@name=$parent.type or @name=substring-after($parent.type,':')]/xsd:sequence/xsd:element[@name=$element.name]/@type!=''"> 
						<xsl:value-of select="$parent.schema.name" />
					</xsl:when> 
					<xsl:otherwise>
						<xsl:for-each select="document($parent.schema.name)//xsd:schema/xsd:include/@schemaLocation">
							<xsl:if test="document(.)//xsd:element[@name=$element.name]/@type!=''"> 
								<xsl:value-of select="." /> 
							</xsl:if> 
						</xsl:for-each> 
					</xsl:otherwise> 
				</xsl:choose> 
			</xsl:when>
			<xsl:when test="$element.prefix!=''"> 
				<xsl:value-of select="document($parent.schema.name)//xsd:import[@namespace=$element.namespace]/@schemaLocation"/> 
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$parent.schema.name" />
			</xsl:otherwise> 
		</xsl:choose> 
	</xsl:variable>

	<!-- filter out possible duplicates -->
	<xsl:variable name="element.schema.name">
		<xsl:value-of select="concat(substring-before($element.schema.name.collect,'.xsd'),'.xsd')" />
	</xsl:variable>

	<xsl:variable name="element.type">
		<xsl:choose>
			<xsl:when test="document($parent.schema.name)//xsd:complexType[@name=$parent.type or @name=substring-after($parent.type,':')]/xsd:sequence/xsd:element[@name=$element.name]/@type!=''"> 
				<xsl:value-of select="document($parent.schema.name)//xsd:complexType[@name=$parent.type or @name=substring-after($parent.type,':')]/xsd:sequence/xsd:element[@name=$element.name]/@type" /> 
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="document($element.schema.name)//xsd:element[@name=$element.name]/@type"/>
			</xsl:otherwise>
		</xsl:choose> 
	</xsl:variable>
		
	<xsl:variable name="element.type.schema.name">
		<xsl:call-template name="GetDataTypeDocument">
			<xsl:with-param name="element.type" select="$element.type" />
			<xsl:with-param name="element.schema.name" select="$element.schema.name" />
		</xsl:call-template>
	</xsl:variable>
	
	<xsl:variable name="den">
		<xsl:choose>
			<xsl:when test="document($parent.schema.name)//xsd:complexType[@name=$parent.type or @name=substring-after($parent.type,':')]/xsd:sequence/xsd:element[@name=$element.name or @ref=concat($element.prefix,':',$element.name)]/xsd:annotation/xsd:documentation/ccts:DictionaryEntryName!=''"> 
<!-- <xsl:value-of select="'Document_ Command_ Header_ Type. Entity_ Identification. Entity_ Identification_ Type'" /> -->
				<xsl:value-of select="document($parent.schema.name)//xsd:complexType[@name=$parent.type or @name=substring-after($parent.type,':')]/xsd:sequence/xsd:element[@name=$element.name or @ref=concat($element.prefix,':',$element.name)]/xsd:annotation/xsd:documentation/ccts:DictionaryEntryName" /> 
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="CamelCase2Words">
					<xsl:with-param name="input" select="$parent.name" />
					<xsl:with-param name="sep" select="'_ '" />
				</xsl:call-template>
				<xsl:value-of select="'. '" />
				<xsl:call-template name="CamelCase2Words">
					<xsl:with-param name="input" select="$element.name" />
					<xsl:with-param name="sep" select="'_ '" />
				</xsl:call-template>
				<xsl:value-of select="'. '" />
				<xsl:choose>
					<xsl:when test="contains($element.type,':')">
						<xsl:call-template name="CamelCase2Words">
							<xsl:with-param name="input" select="substring-after($element.type,':')" />
							<xsl:with-param name="sep" select="'_ '" />
						</xsl:call-template>
					</xsl:when>
					<xsl:otherwise>
						<xsl:call-template name="CamelCase2Words">
							<xsl:with-param name="input" select="$element.type" />
							<xsl:with-param name="sep" select="'_ '" />
						</xsl:call-template>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise> 
		</xsl:choose>		
	</xsl:variable>
		
	<!--	<xsl:variable name="derived.repeat">
					<xsl:call-template name="FindPropertyValue">
						<xsl:with-param name="den.current" select="$den"/>
						<xsl:with-param name="den.start" select="$den"/>
						<xsl:with-param name="property" select="'derived'" />
						<xsl:with-param name="class" select="'content'"/>
						<xsl:with-param name="value" select="'value'"/>
                    <xsl:with-param name="mode" select="$parent.mode"/>
					</xsl:call-template>
		</xsl:variable> -->
		
	<xsl:variable name="annotated.mode">
		<xsl:value-of select="''" />
		<xsl:if test="$parent.mode!='read'" >
			<xsl:call-template name="FindPropertyValue">
				<xsl:with-param name="den.current" select="$den"/>
				<xsl:with-param name="den.start" select="$den"/>
				<xsl:with-param name="property" select="'mode'" />
				<xsl:with-param name="class" select="'core-component'"/>
				<xsl:with-param name="value" select="'value'"/>
				<xsl:with-param name="mode" select="$parent.mode"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:variable>

	<xsl:variable name="element.mode">
		<xsl:choose>
			<xsl:when test="$parent.mode='read'">
				<xsl:value-of select="$parent.mode" />
			</xsl:when>
			<xsl:when test="$annotated.mode!=''">
				<xsl:value-of select="$annotated.mode" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$parent.mode" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
      	
	<xsl:variable name="element.acronym">
		<xsl:value-of select="document($parent.schema.name)//xsd:complexType[@name=$parent.type or @name=substring-after($parent.type,':')]/xsd:sequence/xsd:element[@name=$element.name or @ref=concat($element.prefix,':',$element.name)]/xsd:annotation/xsd:documentation/ccts:Acronym" />
	</xsl:variable>
		
	<xsl:variable name="derpath">
		<xsl:call-template name="FindPropertyValue">
			<xsl:with-param name="den.current" select="$den"/>
			<xsl:with-param name="den.start" select="$den"/>
			<xsl:with-param name="property" select="'derived'" />
			<xsl:with-param name="class" select="'content'"/>
			<xsl:with-param name="value" select="'value'"/>
			<xsl:with-param name="mode" select="$parent.mode"/>
		</xsl:call-template>
	</xsl:variable>
	<xsl:variable name="derivation.path">
			<xsl:copy-of select="msxsl:node-set($parent.derivation.path)/*/*[name()=$derpath][position()=$dercount]" />
	</xsl:variable> 
	<xsl:variable name="derivation.count"> 
			<xsl:value-of select="count(msxsl:node-set($parent.derivation.path)/*/*[name()=$derpath])" />
	</xsl:variable> 
			
	<xsl:element name="ebforms:element">	
			   
		<xsl:attribute name="name">
			<xsl:value-of select="$element.name" />
		</xsl:attribute>
				
		<xsl:attribute name="type">
			<xsl:value-of select="$element.type" /> 
		</xsl:attribute>

		<xsl:attribute name="minOccurs">
			<xsl:choose>
				<xsl:when test="document($parent.schema.name)//xsd:complexType[@name=$parent.type or @name=substring-after($parent.type,':')]/xsd:sequence/xsd:element[@name=$element.name or @ref=concat($element.prefix,':',$element.name)]/@minOccurs!=''">
						<xsl:value-of select="document($parent.schema.name)//xsd:complexType[@name=$parent.type or @name=substring-after($parent.type,':')]/xsd:sequence/xsd:element[@name=$element.name or @ref=concat($element.prefix,':',$element.name)]/@minOccurs" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="'1'" />		
				</xsl:otherwise>
			</xsl:choose>
		</xsl:attribute>
		
		<xsl:attribute name="maxOccurs">
			<xsl:choose>
				<xsl:when test="document($parent.schema.name)//xsd:complexType[@name=$parent.type or @name=substring-after($parent.type,':')]/xsd:sequence/xsd:element[@name=$element.name or @ref=concat($element.prefix,':',$element.name)]/@maxOccurs!=''">
						<xsl:value-of select="document($parent.schema.name)//xsd:complexType[@name=$parent.type or @name=substring-after($parent.type,':')]/xsd:sequence/xsd:element[@name=$element.name or @ref=concat($element.prefix,':',$element.name)]/@maxOccurs" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="'1'" />	
				</xsl:otherwise>
			</xsl:choose>
		</xsl:attribute>
			
		<xsl:element name="ebforms:mode" >
			<xsl:value-of select="$element.mode" /> 
		</xsl:element>
		
		<xsl:element name="ebforms:schema">
			<xsl:value-of select="$element.type.schema.name"/>
		</xsl:element>

	    <xsl:element name="ebforms:ns">
      		<xsl:value-of select="$element.namespace" />
      	</xsl:element>
      	
      	<xsl:element name="ebforms:prefix">
			<xsl:value-of select="$element.prefix" />
		</xsl:element>	
	
		<xsl:element name="ebforms:full-name">
			<xsl:choose>
				<xsl:when test="$element.prefix!=''">
					<xsl:value-of select="concat('eb',$element.prefix,':',$element.name)" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$element.name" />
				</xsl:otherwise>		
			</xsl:choose>
		</xsl:element>	

      	<xsl:element name="ebforms:Acronym">
      		<xsl:value-of select="$element.acronym" />
      	</xsl:element>
			
		<xsl:element name="ebforms:den" >
			<xsl:value-of select="$den" />
		</xsl:element> 
					
		<xsl:variable name="element.show" >
			<xsl:choose>
				<xsl:when test="$parent.show!='false'">
					<xsl:call-template name="FindPropertyValue">
						<xsl:with-param name="den.current" select="$den"/>
						<xsl:with-param name="den.start" select="$den"/>
						<xsl:with-param name="property" select="'show'" />
						<xsl:with-param name="class" select="'core-component'"/>
						<xsl:with-param name="value" select="'value'"/>
						<xsl:with-param name="mode" select="$parent.mode"/>
					</xsl:call-template> 
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$parent.show" />
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
	
		<xsl:element name="ebforms:show" >
			<xsl:value-of select="$element.show" />
		</xsl:element> 

		<xsl:if test="$element.show!='false'">
		
			<xsl:element name="ebforms:businessTerm">
				<xsl:call-template name="getBusinessTerm">
					<xsl:with-param name="den" select="$den" />
					<xsl:with-param name="annotated.business.term" select="document($parent.schema.name)//xsd:complexType[@name=$parent.type or @name=substring-after($parent.type,':')]/xsd:sequence/xsd:element[@name=$element.name or @ref=concat($element.prefix,':',$element.name)]/xsd:annotation/xsd:documentation/ccts:BusinessTerm" />
				</xsl:call-template>
			</xsl:element>

			<xsl:variable name="sequence">
				<xsl:value-of select="''" />
				<xsl:if test="$element.show!='false'">
					<xsl:call-template name="FindPropertyValue">
						<xsl:with-param name="den.current" select="$den"/>
						<xsl:with-param name="den.start" select="$den"/> 
						<xsl:with-param name="property" select="'sequence'" />
						<xsl:with-param name="class" select="'core-component'"/>
						<xsl:with-param name="value" select="'value'"/>
						<xsl:with-param name="mode" select="$parent.mode"/>
					</xsl:call-template>
				</xsl:if>	
			</xsl:variable>

			<xsl:element name="ebforms:sequence">
				<xsl:if test="$sequence=''">
					<xsl:value-of select="'99999'" />
				</xsl:if>
				<xsl:value-of select="$sequence" />
			</xsl:element>

			
			<xsl:variable name="showcaption" >
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'show'" />
					<xsl:with-param name="class" select="'caption'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$parent.mode"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:element name="ebforms:showcaption" >
				<xsl:value-of select="$showcaption" />
			</xsl:element>
			
			 <xsl:element name="ebforms:show-number" >
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'show-number'" />
					<xsl:with-param name="class" select="'core-component'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$parent.mode"/>
				</xsl:call-template>
			</xsl:element>
			 <xsl:element name="ebforms:paging-caption" >
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'show-number'" />
					<xsl:with-param name="class" select="'core-component'"/>
					<xsl:with-param name="value" select="'caption'"/>
					<xsl:with-param name="mode" select="$parent.mode"/>
				</xsl:call-template>
			</xsl:element>
			
			 <xsl:element name="ebforms:add-button" >
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'add'" />
					<xsl:with-param name="class" select="'core-component'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$parent.mode"/>
				</xsl:call-template>
			</xsl:element>
			 <xsl:element name="ebforms:del-button" >
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'del'" />
					<xsl:with-param name="class" select="'core-component'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$parent.mode"/>
				</xsl:call-template>
			</xsl:element>
		
			<xsl:for-each select="$global.annotations.node/ebforms:annotations/ebforms:core-component[
				@den = $den]/ebforms:element[@class='core-component']/ebforms:search" >
				<xsl:element name="ebforms:search" >
					<xsl:attribute name="caption">
						<xsl:value-of select="@caption" />
					</xsl:attribute>
					<xsl:value-of select="@value" />
				</xsl:element>
			</xsl:for-each>
		
		<xsl:if test="$element.acronym='BBIE' or $element.acronym='SC' ">
			<!-- here include the schemas or files with enumeration -->
			<!-- also reference to enum in ann file -->
			<xsl:variable name="enum" select="$global.annotations.node/ebforms:annotations/ebforms:core-component[
				@den = $den]/ebforms:element[@class='content']/ebforms:enum/@value" />
			<xsl:variable name="enumschema.file">
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'enumschema'" />
					<xsl:with-param name="class" select="'content'"/>
					<xsl:with-param name="value" select="'file'"/>
					<xsl:with-param name="mode" select="$parent.mode"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:choose>
				<xsl:when test="$enum!=''">
					<xsl:element name="ebforms:enum">
						<xsl:attribute name="file">
							<xsl:value-of select="'annotations.xml'" />
						</xsl:attribute>
						<xsl:attribute name="nodepath">
							<xsl:value-of select="concat('//ebforms:core-component[@den =', $quot,$den,$quot,']/ebforms:element[@class=',$quot,'content',$quot,']/ebforms:enum')" />
						</xsl:attribute>
						<xsl:attribute name="namepath">
							<xsl:value-of select="'@name'" />
						</xsl:attribute>
						<xsl:attribute name="valuepath">
							<xsl:value-of select="'@value'" />
						</xsl:attribute>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$global.annotations.node/ebforms:annotations/ebforms:core-component[
				@den = $den]/ebforms:element[@class='content']/ebforms:enumschema/@file!=''">
					<xsl:for-each select="$global.annotations.node/ebforms:annotations/ebforms:core-component[
						@den = $den]/ebforms:element[@class='content']/ebforms:enumschema">
						<xsl:element name="ebforms:enum">
							<xsl:attribute name="file">
								<xsl:value-of select="@file" />
							</xsl:attribute>
							<xsl:attribute name="nodepath">
								<xsl:value-of select="@nodepath" />
							</xsl:attribute>
							<xsl:attribute name="namepath">
								<xsl:value-of select="@namepath" />
							</xsl:attribute>
							<xsl:attribute name="valuepath">
								<xsl:value-of select="@valuepath" />
							</xsl:attribute>
						</xsl:element>
					</xsl:for-each>
				</xsl:when> 
	<!--		<xsl:when test="document($element.schema)//xsd:simpleType[@name=$type or @name=substring-after($type,':')]/xsd:restriction/xsd:enumeration/@value!=''"> 
					<xsl:element name="ebforms:enum">
						<xsl:attribute name="file">
							<xsl:value-of select="$element.schema" />
						</xsl:attribute>
						<xsl:attribute name="valuepath">
						</xsl:attribute>
						<xsl:attribute name="namepath">
						</xsl:attribute>
						 <xsl:attribute name="basetype">
							 <xsl:value-of select="$type" />
						 </xsl:attribute> 
						 <xsl:value-of select="$element.schema"/>
					</xsl:element>
				</xsl:when> -->
			</xsl:choose>
			<xsl:if test="$global.annotations.node/ebforms:annotations/ebforms:core-component[
				@den = $den]/ebforms:element[@class='content']/ebforms:enumdependency/@attribute!=''">
				<xsl:copy-of select="$global.annotations.node/ebforms:annotations/ebforms:core-component[
				@den = $den]/ebforms:element[@class='content']/ebforms:enumdependency" />
			</xsl:if>
			<xsl:variable name="element.base" select="document($element.type.schema.name)//xsd:complexType[@name=$element.type or @name=substring-after($element.type,':')]/xsd:simpleContent/xsd:extension/@base" />
			<xsl:if test="$element.base!='' and substring-before($element.type,':')='qdt'">
				<xsl:variable name="element.base.ns" select="document($element.type.schema.name)/xsd:schema/namespace::*[name() = substring-before($element.base,':')]" />
				<xsl:element name="ebforms:enumSchema">
					 <xsl:attribute name="basetype">
						 <xsl:value-of select="$element.base" />
					 </xsl:attribute> 
					 <xsl:value-of select="document($element.type.schema.name)//xsd:import[@namespace=$element.base.ns]/@schemaLocation"/>
				</xsl:element>
			</xsl:if> 
		</xsl:if>
		<!-- BBIE enum-->

		<xsl:variable name="element.width">
			<xsl:call-template name="FindPropertyValue">
				<xsl:with-param name="den.current" select="$den"/>
				<xsl:with-param name="den.start" select="$den"/>
				<xsl:with-param name="property" select="'width'" />
				<xsl:with-param name="class" select="'core-component'"/>
				<xsl:with-param name="value" select="'value'"/>
				<xsl:with-param name="mode" select="$parent.mode"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:if test="$element.width!=''">	
			<xsl:element name="ebforms:width">
				<xsl:value-of select="$element.width" />
			</xsl:element>		
		</xsl:if>	
					
		<xsl:variable name="element.class">
			<xsl:call-template name="FindPropertyValue">
				<xsl:with-param name="den.current" select="$den"/>
				<xsl:with-param name="den.start" select="$den"/>
				<xsl:with-param name="property" select="'class'" />
				<xsl:with-param name="class" select="'core-component'"/>
				<xsl:with-param name="value" select="'value'"/>
				<xsl:with-param name="mode" select="$parent.mode"/>
			</xsl:call-template>
		</xsl:variable>				
		<xsl:if test="$element.class!=''">	
			<xsl:element name="ebforms:class">
				<xsl:value-of select="$element.class" />
			</xsl:element>		
		</xsl:if>	

		<xsl:variable name="caption.width">
			<xsl:call-template name="FindPropertyValue">
				<xsl:with-param name="den.current" select="$den"/>
				<xsl:with-param name="den.start" select="$den"/>
				<xsl:with-param name="property" select="'width'" />
				<xsl:with-param name="class" select="'caption'"/>
				<xsl:with-param name="value" select="'value'"/>
				<xsl:with-param name="mode" select="$parent.mode"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:if test="$caption.width!=''">	
			<xsl:element name="ebforms:caption-width">
				<xsl:value-of select="$caption.width" />
			</xsl:element>		
		</xsl:if>	
						
		<xsl:variable name="caption.class">
			<xsl:call-template name="FindPropertyValue">
				<xsl:with-param name="den.current" select="$den"/>
				<xsl:with-param name="den.start" select="$den"/>
				<xsl:with-param name="property" select="'class'" />
				<xsl:with-param name="class" select="'caption'"/>
				<xsl:with-param name="value" select="'value'"/>
				<xsl:with-param name="mode" select="$parent.mode"/>
			</xsl:call-template>
		</xsl:variable>				
		<xsl:if test="$caption.class!=''">	
			<xsl:element name="ebforms:caption-class">
				<xsl:value-of select="$caption.class" />
			</xsl:element>		
		</xsl:if>	

		<xsl:if test="$element.acronym='ASBIE' ">

			<xsl:element name="ebforms:show-children" >
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'show-children'" />
					<xsl:with-param name="class" select="'core-component'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$parent.mode"/>
				</xsl:call-template>
			</xsl:element>

			<xsl:variable name="hulpfield">
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'field'" />
					<xsl:with-param name="class" select="'core-component'"/>
					<xsl:with-param name="value" select="'name'"/>
					<xsl:with-param name="mode" select="$parent.mode"/>
				</xsl:call-template>
			</xsl:variable>
		
			<xsl:if test="$hulpfield!=''">
				<xsl:element name="ebforms:field" >
					<xsl:attribute name="calculate">
						<xsl:call-template name="FindPropertyValue">
							<xsl:with-param name="den.current" select="$den"/>
							<xsl:with-param name="den.start" select="$den"/>
							<xsl:with-param name="property" select="'field'" />
							<xsl:with-param name="class" select="'core-component'"/>
							<xsl:with-param name="value" select="'calculate'"/>
							<xsl:with-param name="mode" select="$parent.mode"/>
						</xsl:call-template>
					</xsl:attribute>
					<xsl:attribute name="caption">
						<xsl:call-template name="FindPropertyValue">
							<xsl:with-param name="den.current" select="$den"/>
							<xsl:with-param name="den.start" select="$den"/>
							<xsl:with-param name="property" select="'field'" />
							<xsl:with-param name="class" select="'core-component'"/>
							<xsl:with-param name="value" select="'caption'"/>
							<xsl:with-param name="mode" select="$parent.mode"/>
						</xsl:call-template>
					</xsl:attribute>
					<xsl:value-of select="$hulpfield" />
				</xsl:element>
			</xsl:if> 

			<xsl:element name="ebforms:image">
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'image'" />
					<xsl:with-param name="class" select="'content'"/>
					<xsl:with-param name="value" select="'path'"/>
					<xsl:with-param name="mode" select="$parent.mode"/>
				</xsl:call-template>
			</xsl:element>
			
			<xsl:variable name="select">
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'select'" />
					<xsl:with-param name="class" select="'core-component'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$parent.mode"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:if test="$select!=''">
				<xsl:element name="ebforms:select" >
					<xsl:attribute name="instance" >
						<xsl:call-template name="FindPropertyValue">
							<xsl:with-param name="den.current" select="$den"/>
							<xsl:with-param name="den.start" select="$den"/>
							<xsl:with-param name="property" select="'select'" />
							<xsl:with-param name="class" select="'core-component'"/>
							<xsl:with-param name="value" select="'instance'"/>
							<xsl:with-param name="mode" select="$parent.mode"/>
						</xsl:call-template>
					</xsl:attribute>
					<xsl:attribute name="caption" >
						<xsl:call-template name="FindPropertyValue">
							<xsl:with-param name="den.current" select="$den"/>
							<xsl:with-param name="den.start" select="$den"/>
							<xsl:with-param name="property" select="'select'" />
							<xsl:with-param name="class" select="'core-component'"/>
							<xsl:with-param name="value" select="'caption'"/>
							<xsl:with-param name="mode" select="$parent.mode"/>
						</xsl:call-template>
					</xsl:attribute> 
					<xsl:value-of select="$select" />
				</xsl:element>
			</xsl:if> 
		</xsl:if>
		<!-- ASBIE -->
		
		<xsl:variable name="add-path">
			<xsl:call-template name="FindPropertyValue">
				<xsl:with-param name="den.current" select="$den"/>
				<xsl:with-param name="den.start" select="$den"/>
				<xsl:with-param name="property" select="'add-trigger'" />
				<xsl:with-param name="class" select="'core-component'"/>
				<xsl:with-param name="value" select="'path'"/>
				<xsl:with-param name="mode" select="$parent.mode"/>
			</xsl:call-template>
		</xsl:variable>
	
		<xsl:if test="$add-path!=''">
			<xsl:element name="ebforms:add-path" >
				<xsl:attribute name="target">
					<xsl:call-template name="FindPropertyValue">
						<xsl:with-param name="den.current" select="$den"/>
						<xsl:with-param name="den.start" select="$den"/>
						<xsl:with-param name="property" select="'add-trigger'" />
						<xsl:with-param name="class" select="'core-component'"/>
						<xsl:with-param name="value" select="'target'"/>
						<xsl:with-param name="mode" select="$parent.mode"/>
					</xsl:call-template>
				</xsl:attribute>
				<xsl:attribute name="test-empty" >
					<xsl:call-template name="FindPropertyValue">
						<xsl:with-param name="den.current" select="$den"/>
						<xsl:with-param name="den.start" select="$den"/>
						<xsl:with-param name="property" select="'add-trigger'" />
						<xsl:with-param name="class" select="'core-component'"/>
						<xsl:with-param name="value" select="'test-empty'"/>
						<xsl:with-param name="mode" select="$parent.mode"/>
					</xsl:call-template>
				</xsl:attribute>
				<xsl:attribute name="position" >
					<xsl:call-template name="FindPropertyValue">
						<xsl:with-param name="den.current" select="$den"/>
						<xsl:with-param name="den.start" select="$den"/>
						<xsl:with-param name="property" select="'add-trigger'" />
						<xsl:with-param name="class" select="'core-component'"/>
						<xsl:with-param name="value" select="'position'"/>
						<xsl:with-param name="mode" select="$parent.mode"/>
					</xsl:call-template>
				</xsl:attribute>
				<xsl:value-of select="$add-path" />
			</xsl:element>
		</xsl:if>
		<!-- add-path -->
		
		<xsl:element name="ebforms:control" >
			<xsl:call-template name="FindPropertyValue">
				<xsl:with-param name="den.current" select="$den"/>
				<xsl:with-param name="den.start" select="$den"/>
				<xsl:with-param name="property" select="'control'" />
				<xsl:with-param name="class" select="'core-component'"/>
				<xsl:with-param name="value" select="'value'"/>
				<xsl:with-param name="mode" select="$parent.mode"/>
			</xsl:call-template>
		</xsl:element>

		<xsl:if test="$element.acronym='BBIE' or $element.acronym='SC' ">
			
			<xsl:variable name="content.width">
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'width'" />
					<xsl:with-param name="class" select="'content'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$parent.mode"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:if test="$content.width!=''">	
				<xsl:element name="ebforms:content-width">
					<xsl:value-of select="$content.width" />
				</xsl:element>		
			</xsl:if>	

			
			<xsl:variable name="content.class">
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'class'" />
					<xsl:with-param name="class" select="'content'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$parent.mode"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:if test="$content.class!=''">	
				<xsl:element name="ebforms:content-class">
					<xsl:value-of select="$content.class" />
				</xsl:element>		
			</xsl:if>	

			<xsl:variable name="required">
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'required'" />
					<xsl:with-param name="class" select="'content'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$parent.mode"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:if test="$required!=''" >
				<xsl:element name="ebforms:required">
					<xsl:value-of select="$required" />
				</xsl:element>
			</xsl:if>
		</xsl:if>
		<!-- BBIE -->

	</xsl:if>
	<!-- show -->
	
	<xsl:if test="$element.acronym='BBIE' or $element.acronym='SC' ">
		<xsl:variable name="setvalue">
			<xsl:call-template name="FindPropertyValue">
				<xsl:with-param name="den.current" select="$den"/>
				<xsl:with-param name="den.start" select="$den"/>
				<xsl:with-param name="property" select="'setvalue'" />
				<xsl:with-param name="class" select="'core-component'"/>
				<xsl:with-param name="value" select="'path'"/>
				<xsl:with-param name="mode" select="$parent.mode"/>
			</xsl:call-template>
		</xsl:variable>
	
		<xsl:if test="$setvalue!=''">
			<xsl:element name="ebforms:setvalue" >
				<xsl:value-of select="$setvalue" />
			</xsl:element>
		</xsl:if>
		
		<xsl:variable name="default">
			<xsl:call-template name="FindPropertyValue">
				<xsl:with-param name="den.current" select="$den" />
				<xsl:with-param name="den.start"  select="$den" />
				<xsl:with-param name="property" select="'default'" />
				<xsl:with-param name="class" select="'content'" />
				<xsl:with-param name="value" select="'value'"/>
				<xsl:with-param name="mode" select="$parent.mode"/>
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:variable name="default.calculate">
			<xsl:call-template name="FindPropertyValue">
				<xsl:with-param name="den.current" select="$den"/>
				<xsl:with-param name="den.start" select="$den"/>
				<xsl:with-param name="property" select="'default'" />
				<xsl:with-param name="class" select="'content'"/>
				<xsl:with-param name="value" select="'calculate'"/>
				<xsl:with-param name="mode" select="$parent.mode"/>
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:element name="ebforms:default">
			<xsl:choose>
				<xsl:when test="$element.mode='read' and .!=''">
					<xsl:value-of select="." />
				</xsl:when>
				<xsl:when test="$default.calculate!=''" >
					<xsl:attribute name="calculate">
						<xsl:value-of select="$default.calculate" />
					</xsl:attribute>
				</xsl:when>
				<xsl:when test=".!=''">
					<xsl:value-of select="." />
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$default" />
					<xsl:if test="$default='' and $level=2 and $derivation.count &gt; 0">
						<xsl:value-of select="$derivation.path" />
					</xsl:if>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:element> 
		<xsl:element name="ebforms:constraint">
			<xsl:call-template name="FindPropertyValue">
				<xsl:with-param name="den.current" select="$den"/>
				<xsl:with-param name="den.start" select="$den"/>
				<xsl:with-param name="property" select="'constraint'" />
				<xsl:with-param name="class" select="'content'"/>
				<xsl:with-param name="value" select="'value'"/>
				<xsl:with-param name="mode" select="$parent.mode"/>
			</xsl:call-template>
		</xsl:element>

		<xsl:element name="ebforms:calculate">
			<xsl:call-template name="FindPropertyValue">
				<xsl:with-param name="den.current" select="$den"/>
				<xsl:with-param name="den.start" select="$den"/>
				<xsl:with-param name="property" select="'calculate'" />
				<xsl:with-param name="class" select="'content'"/>
				<xsl:with-param name="value" select="'value'"/>
				<xsl:with-param name="mode" select="$parent.mode"/>
			</xsl:call-template>
		</xsl:element>
	</xsl:if>
	<!-- BBIE -->
	
	<!--	<xsl:element name="ebforms:derived">
			<xsl:attribute name="count">
				<xsl:value-of select="$derivation.count" />
			</xsl:attribute>
			<xsl:copy-of select="$derivation.path" />
		</xsl:element> -->
				
		<xsl:call-template name="GetAttributes">
			<xsl:with-param name="element.type" select="$element.type" />
			<xsl:with-param name="element.schema.name" select="$element.type.schema.name" />
			<xsl:with-param name="schema.node" select="document($element.type.schema.name)/xsd:schema//*[@name=substring-after($element.type,':')]" />
			<xsl:with-param name="instance.node" select="."/>
			<xsl:with-param name="element.name" select="$element.name" />
			<xsl:with-param name="parent.derivation.path"  select="$derivation.path" />
			<xsl:with-param name="mode"  select="$element.mode" />
		</xsl:call-template> 
		
		<xsl:for-each select="./*[$level!='3' or name(.)!=name(preceding-sibling::*[1])]"> 						
			   <xsl:call-template name="GetElementType">
					<xsl:with-param name="parent.type" select="$element.type" />
					<xsl:with-param name="parent.schema.name"  select="$element.type.schema.name"/>
					<xsl:with-param name="parent.schema.node"  select="document($element.type.schema.name)/xsd:schema//*[@name=substring-after($element.type,':')]" />
					<xsl:with-param name="parent.instance.node"  select="."/>
					<xsl:with-param name="parent.name"  select="$element.name"/>
					<xsl:with-param name="parent.derivation.path"  select="$derivation.path" />
					<xsl:with-param name="level"  select="$level"/>
					<xsl:with-param name="dercount"  select="1"/>
					<xsl:with-param name="parent.mode" select="$element.mode" />
					<xsl:with-param name="parent.show" select="$element.show" /> 
			</xsl:call-template> 
		</xsl:for-each> 
		
		
		
	<!--	<xsl:call-template name="GetElementTypes">
			<xsl:with-param name="element.type" select="$element.type" />
			<xsl:with-param name="element.schema.name" select="$element.type.schema.name" />
			<xsl:with-param name="schema.node" select="document($element.type.schema.name)/xsd:schema//*[@name=substring-after($element.type,':')]" />
			<xsl:with-param name="instance.node" select="."/>
			<xsl:with-param name="element.name" select="$element.name" />
			<xsl:with-param name="parent.derivation.path"  select="$derivation.path" />
			<xsl:with-param name="level" select="$level"/>
			<xsl:with-param name="dercount" select="1"/>
			<xsl:with-param name="parent.mode" select="$element.mode"/>
			<xsl:with-param name="parent.show" select="$element.show"/>
		</xsl:call-template> -->
		
	<!--	<xsl:variable name="helpfield">
			<xsl:call-template name="FindPropertyValue">
				<xsl:with-param name="den.current" select="$den"/>
				<xsl:with-param name="den.start" select="$den"/>
				<xsl:with-param name="property" select="'field'" />
				<xsl:with-param name="class" select="'core-component'"/>
				<xsl:with-param name="value" select="'name'"/>
				<xsl:with-param name="mode" select="'new'"/>

			</xsl:call-template>
		</xsl:variable>-->
	</xsl:element>	
	
	<xsl:if test="($level!='3' and $dercount &lt; $derivation.count) ">
		<xsl:call-template name="GetElementType">
			<xsl:with-param name="element.type" select="$element.type" />
			<xsl:with-param name="element.schema.name" select="$element.schema.name" />
			<xsl:with-param name="schema.node" select="document($element.type.schema.name)/xsd:schema//*[@name=substring-after($element.type,':')]" />
			<xsl:with-param name="instance.node" select="."/>
			<xsl:with-param name="element.name" select="$element.name" />
			<xsl:with-param name="parent.derivation.path"  select="$parent.derivation.path" />
			<xsl:with-param name="level" select="$level"/>
			<xsl:with-param name="dercount" select="$dercount + 1"/>
			<xsl:with-param name="parent.mode" select="$element.mode" />
			<xsl:with-param name="parent.show" select="$parent.show" /> 
		</xsl:call-template>
	</xsl:if> 
	
</xsl:template>

<xsl:template name="GetAttributes">
    <xsl:param name="element.type" />
    <xsl:param name="element.schema.name" />
    <xsl:param name="schema.node" />
    <xsl:param name="instance.node" />
    <xsl:param name="element.name" />
    <xsl:param name="parent.derivation.path" />
    <xsl:param name="mode"/>

	<!-- select all attributes present in instance -->
	<xsl:for-each select="@*">

		<xsl:variable name="instance.name">
			<xsl:value-of select="name(.)" />
		</xsl:variable> 

		<xsl:variable name="schema.element.node">
			<xsl:copy-of select="msxsl:node-set($schema.node)//xsd:simpleContent/xsd:extension/xsd:attribute[@name=$instance.name]|msxsl:node-set($schema.node)/xsd:attribute[@name=$instance.name]" />
		</xsl:variable>
			
		<xsl:variable name="element.child.type" select="msxsl:node-set($schema.element.node)/xsd:attribute/@type" />
		 
		<xsl:variable name="element.type.schema.name">
			<xsl:if test="substring-before($element.child.type,':')!='xsd'" >
				<xsl:call-template name="GetDataTypeDocument">
					<xsl:with-param name="element.type" select="$element.child.type" />
					<xsl:with-param name="element.schema.name" select="$element.schema.name" />
				</xsl:call-template>
			</xsl:if>
		</xsl:variable>
		
		<xsl:variable name="den">
			<xsl:choose>
				<xsl:when test="msxsl:node-set($schema.element.node)/xsd:attribute/xsd:annotation/xsd:documentation/ccts:DictionaryEntryName!=''">
					<xsl:value-of select="msxsl:node-set($schema.element.node)/xsd:attribute/xsd:annotation/xsd:documentation/ccts:DictionaryEntryName" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="CamelCase2Words">
						<xsl:with-param name="input" select="$instance.name" />
						<xsl:with-param name="sep" select="'_ '" />
					</xsl:call-template>
					<xsl:value-of select="'. Type'" />
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable> 
		
		<xsl:element name="ebforms:attribute">
			
			<xsl:attribute name="name">
				<xsl:value-of select="$instance.name" />
			</xsl:attribute>
			<xsl:attribute name="type">
				<xsl:value-of select="$element.child.type" /> 
			</xsl:attribute>
						
			<xsl:element name="ebforms:full-name">
				<xsl:value-of select="$instance.name" />
			</xsl:element> 

			<xsl:element name="ebforms:schema">
				<xsl:value-of select="$element.type.schema.name" />
			</xsl:element> 
			
			<xsl:if test="document($element.type.schema.name)//
					xsd:simpleType[@name=$element.child.type or @name=substring-after($element.child.type,':')]/xsd:restriction/xsd:enumeration/@value!=''"> 
				<xsl:element name="ebforms:enumSchema">
					 <xsl:attribute name="basetype">
						 <xsl:value-of select="$element.child.type" />
					 </xsl:attribute> 
					 <xsl:value-of select="$element.type.schema.name"/>
				</xsl:element>
			</xsl:if> 
			
			<xsl:element name="ebforms:Acronym">
				<xsl:choose>
					<xsl:when test="msxsl:node-set($schema.element.node)/xsd:attribute/xsd:annotation/xsd:documentation/ccts:Acronym!=''">
						<xsl:value-of select="msxsl:node-set($schema.element.node)/xsd:attribute/xsd:annotation/xsd:documentation/ccts:Acronym" />
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="'SC'" />
					</xsl:otherwise>
				</xsl:choose>
			</xsl:element>

			<xsl:element name="ebforms:den">
				<xsl:value-of select="$den" />
			</xsl:element> 

<!--			<xsl:element name="ebforms:strippedDen">
				<xsl:call-template name="GetStrippedDen">
					<xsl:with-param name="den" select="$den" />
				</xsl:call-template>
			</xsl:element> -->

			<xsl:element name="ebforms:businessTerm">
				<xsl:call-template name="getBusinessTerm">
					<xsl:with-param name="den" select="$den" />
					<xsl:with-param name="annotated.business.term" select="msxsl:node-set($schema.element.node)/xsd:attribute/xsd:annotation/xsd:documentation/ccts:BusinessTerm" />
				</xsl:call-template>
			</xsl:element>

			<xsl:element name="ebforms:sequence">
				<xsl:choose>
					<xsl:when test="$global.annotations.node/ebforms:annotations/ebforms:core-component
							[@den=$den]/element[@class='core-component']/ebforms:sequence/@value">
						<xsl:value-of select="$global.annotations.node/ebforms:annotations/ebforms:core-component
							[@den = $den]/element[@class='core-component']/ebforms:sequence/@value" />
					</xsl:when>
					<xsl:otherwise>99999</xsl:otherwise>
				</xsl:choose>
			</xsl:element>
			
			<xsl:variable name="annotated.mode" >
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'mode'" />
					<xsl:with-param name="class" select="'core-component'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$mode"/>
				</xsl:call-template>
			</xsl:variable>

			<xsl:variable name="attribute.mode">
				<xsl:choose>
					<xsl:when test="$mode='read'">
						<xsl:value-of select="$mode" />
					</xsl:when>	
					<xsl:when test="$annotated.mode!=''">
						<xsl:value-of select="$annotated.mode" />
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$mode" />
					</xsl:otherwise>
				</xsl:choose>	
			</xsl:variable>
			
			<xsl:element name="ebforms:parent-mode" >
				<xsl:value-of select="$mode" />
			</xsl:element>


			<xsl:element name="ebforms:annotated-mode" >
				<xsl:value-of select="$annotated.mode" />
			</xsl:element>
		
			<xsl:element name="ebforms:mode" >
				<xsl:value-of select="$attribute.mode" />
			</xsl:element>

			<xsl:element name="ebforms:show" >
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'show'" />
					<xsl:with-param name="class" select="'core-component'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$mode"/>
				</xsl:call-template>
			</xsl:element>				
			
			<xsl:element name="ebforms:default">
				<xsl:value-of select="." />
					<xsl:if test=".=''">
						<xsl:value-of select="msxsl:node-set($parent.derivation.path)/*/@*[name()=$instance.name]"/>
					</xsl:if>
			</xsl:element>
			
		<!--	<xsl:element name="ebforms:default">
				<xsl:choose>
					<xsl:when test=".!=''">
						<xsl:value-of select="." />
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$default" />
						<xsl:if test="$default='' and $level=2 and $derivation.count &gt; 0">
							<xsl:value-of select="$derivation.path" />
						</xsl:if>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:element> -->
			
			<xsl:element name="ebforms:calculate">
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'calculate'" />
					<xsl:with-param name="class" select="'content'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$mode"/>
				</xsl:call-template>
			</xsl:element>	
			
			<xsl:variable name="element.width">
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'width'" />
					<xsl:with-param name="class" select="'core-component'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$mode"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:if test="$element.width!=''">	
				<xsl:element name="ebforms:width">
					<xsl:value-of select="$element.width" />
				</xsl:element>		
			</xsl:if> 	
					
			<xsl:variable name="element.class">
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'class'" />
					<xsl:with-param name="class" select="'core-component'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$mode"/>
				</xsl:call-template>
			</xsl:variable>				
			<xsl:if test="$element.class!=''">	
				<xsl:element name="ebforms:class">
					<xsl:value-of select="$element.class" />
				</xsl:element>		
			</xsl:if>	

			<xsl:variable name="caption.width">
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'width'" />
					<xsl:with-param name="class" select="'caption'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$mode"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:if test="$caption.width!=''">	
				<xsl:element name="ebforms:caption-width">
					<xsl:value-of select="$caption.width" />
				</xsl:element>		
			</xsl:if>	
						
			<xsl:variable name="caption.class">
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'class'" />
					<xsl:with-param name="class" select="'caption'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$mode"/>
				</xsl:call-template>
			</xsl:variable>				
			<xsl:if test="$caption.class!=''">	
				<xsl:element name="ebforms:caption-class">
					<xsl:value-of select="$caption.class" />
				</xsl:element>		
			</xsl:if>	

			<xsl:variable name="content.width">
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'width'" />
					<xsl:with-param name="class" select="'content'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$mode"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:if test="$content.width!=''">	
				<xsl:element name="ebforms:content-width">
					<xsl:value-of select="$content.width" />
				</xsl:element>		
			</xsl:if>	
			
			<xsl:variable name="content.class">
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'class'" />
					<xsl:with-param name="class" select="'content'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$mode"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:if test="$content.class!=''">	
				<xsl:element name="ebforms:content-class">
					<xsl:value-of select="$content.class" />
				</xsl:element>		
			</xsl:if>	
				
			<xsl:variable name="required">
				<xsl:call-template name="FindPropertyValue">
					<xsl:with-param name="den.current" select="$den"/>
					<xsl:with-param name="den.start" select="$den"/>
					<xsl:with-param name="property" select="'required'" />
					<xsl:with-param name="class" select="'content'"/>
					<xsl:with-param name="value" select="'value'"/>
					<xsl:with-param name="mode" select="$mode"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:if test="$required!=''" >
				<xsl:element name="ebforms:required">
					<xsl:value-of select="$required" />
				</xsl:element>
			</xsl:if> 
							
		</xsl:element>
    </xsl:for-each>
</xsl:template>

  <xsl:template name="GetCodelistTypes">
    <xsl:param name="types.used" />
    
    <xsl:for-each select="$types.used">
      <xsl:variable name="element.type.simple" >
        <xsl:call-template name="BasedOnSimpleType">
          <xsl:with-param name="element.type" select="@name" />
          <xsl:with-param name="element.type.schema" select="@schema" />
        </xsl:call-template>
      </xsl:variable>

      <xsl:choose>
        <xsl:when test="$element.type.simple and contains( @name, 'qdt' )">
          <xsl:variable name="type.name" select="substring-after( @name, ':' )" />
          <xsl:variable name="list.content.type">
            <xsl:choose>
              <xsl:when test="document( @schema )/xsd:schema/xsd:simpleType[@name = $type.name]/xsd:restriction/@base">
                <xsl:value-of select="document( @schema )/xsd:schema/xsd:simpleType[@name = $type.name]/xsd:restriction/@base" />
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="document( @schema )/xsd:schema/xsd:complexType[@name = $type.name]/xsd:simpleContent/xsd:extension/@base" />
              </xsl:otherwise>
            </xsl:choose>
          </xsl:variable>

          <xsl:variable name="list.content.type.schema" >
            <xsl:call-template name="GetDataTypeDocument">
              <xsl:with-param name="element.type" select= "$list.content.type" />
              <xsl:with-param name="element.schema.name" select="@schema" />
            </xsl:call-template>
          </xsl:variable>
          <!--
						This is not realy an attribute but a CodeType or IDType which is directly based on
						some content type. This makes the type less or more an attribute itself.
					-->
          <xsl:element name="attribute">
            <xsl:attribute name="name">
              <xsl:value-of select="@name" />
            </xsl:attribute>
            <xsl:attribute name="type">
              <xsl:value-of select="$list.content.type" />
            </xsl:attribute>
            <xsl:attribute name="type.schema">
              <xsl:value-of select="$list.content.type.schema" />
            </xsl:attribute>
          </xsl:element>
        </xsl:when>
        <xsl:otherwise>
          <xsl:variable name="type" select="@name" />
          <xsl:variable name="type.schema" select="@schema" />

          <xsl:variable name="type.attributes.used">
  <!--          <xsl:call-template name="GetAttributeTypes">
              <xsl:with-param name="type" select="$type" />
              <xsl:with-param name="type.schema" select="$type.schema" />
            </xsl:call-template> -->
          </xsl:variable>

          <xsl:for-each select="$type.attributes.used/attribute">
            <xsl:variable name="type.attribute.info">
              <xsl:call-template name="GetAttributeInfo">
                <xsl:with-param name="type" select="$type" />
                <xsl:with-param name="type.schema" select="$type.schema" />
                <xsl:with-param name="type.attribute" select="@name" />
              </xsl:call-template>
            </xsl:variable>
            <!--
							For now we assume that if a type has a schema it's a codelist type. This
							is at all not secure and should be changed!
						-->
            <xsl:if test="not( $type.attribute.info/attribute/@type.schema = '' )">
              <xsl:copy-of select="$type.attribute.info" />
            </xsl:if>
          </xsl:for-each>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>
  
 <xsl:template name="CamelCase2Words">
	 <xsl:param name="input" />
	 <xsl:param name="sep" />
     <xsl:value-of select="translate(substring($input,1,1),'abcdefghijklmnopqrstuwvxyz','ABCDEFGHIJKLMNOPQRSTUWVXYZ')" />
	<xsl:call-template name="CC2W">
		<xsl:with-param name="input" select="substring($input,2,string-length($input)-1)" />
		<xsl:with-param name="sep" select="$sep" />
		<xsl:with-param name="prevUC" select="true()" />
	</xsl:call-template>
</xsl:template>

<xsl:template name="CC2W">
	 <xsl:param name="input" />
	 <xsl:param name="sep" />
	 <xsl:param name="prevUC" />	 
	<xsl:variable name="UC" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
	<xsl:choose>
		<xsl:when test="string-length($input)=0">
		</xsl:when>
		<xsl:when test="contains($UC,substring($input,1,1)) and $prevUC=false()">
			<xsl:value-of select="concat($sep,substring($input,1,1))" />
			<xsl:call-template name="CC2W">
				<xsl:with-param name="input" select="substring($input,2,string-length($input)-1)" />
				<xsl:with-param name="sep" select="$sep" />
				<xsl:with-param name="prevUC" select="true()" />
			</xsl:call-template> 
		</xsl:when>
		<xsl:when test="contains($UC,substring($input,1,1)) and $prevUC=true() and contains($UC,substring($input,2,1))">
			<xsl:value-of select="substring($input,1,1)" />
			<xsl:call-template name="CC2W">
				<xsl:with-param name="input" select="substring($input,2,string-length($input)-1)" />
				<xsl:with-param name="sep" select="$sep" />
				<xsl:with-param name="prevUC" select="true()" />
			</xsl:call-template> 
		</xsl:when>
		<xsl:when test="contains($UC,substring($input,1,1)) and $prevUC=true() and not(contains($UC,substring($input,2,1)))">
			<xsl:value-of select="concat($sep,substring($input,1,2))" />
			<xsl:call-template name="CC2W">
				<xsl:with-param name="input" select="substring($input,3,string-length($input)-2)" />
				<xsl:with-param name="sep" select="$sep" />
				<xsl:with-param name="prevUC" select="false()" />
			</xsl:call-template> 
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="substring($input,1,1)" />
			<xsl:call-template name="CC2W">
				<xsl:with-param name="input" select="substring($input,2,string-length($input)-1)" />
				<xsl:with-param name="sep" select="$sep" />
				<xsl:with-param name="prevUC" select="false()" />
			</xsl:call-template>
		</xsl:otherwise>
	</xsl:choose>	 
 </xsl:template> 
  
<!-- template to recursively write the elements of an instance -->  
<xsl:template name="write.instance">
	<xsl:param name="path" />
	
	<xsl:for-each select="msxsl:node-set($path)/*[name()='ebforms:element']">
		<xsl:variable name="element.name">
			<xsl:value-of select="@name" />
		</xsl:variable> 
		 <xsl:variable name="ns" select="./*[name()='ebforms:ns']" /> 
<!--		 <xsl:element name="{$element.name}" namespace="{$ns}"> -->
<!--		 <xsl:element name="{concat('eb',ebforms:prefix,':',$element.name)}" > -->
						
		 <xsl:choose> 
			<xsl:when test="ebforms:prefix!=''"> 
				<xsl:element name="{concat(ebforms:prefix,':',$element.name)}" > 
					<xsl:for-each select="ebforms:attribute">
						<xsl:variable name="attname" select="@name"/>
						<xsl:attribute name="{$attname}">
							<xsl:value-of select="ebforms:default"/>
						</xsl:attribute>
					</xsl:for-each>
					<!-- <xsl:element name="{$name}"> -->
						<xsl:value-of select="ebforms:default" />
					<xsl:call-template name="write.instance">	
						<xsl:with-param name="path" select="." /> 
					</xsl:call-template>
				</xsl:element>				
			</xsl:when>
			<xsl:otherwise> 
<!--				<xsl:element name="{$element.name}">  -->
<!--				<xsl:element name="{$element.name}" namespace="{$ns}"> -->
				<xsl:element name="{$element.name}" namespace=""> 
					<xsl:for-each select="ebforms:attribute">
						<xsl:variable name="attname" select="@name"/>
						<xsl:attribute name="{$attname}">
							<xsl:value-of select="ebforms:default"/>
						</xsl:attribute>
					</xsl:for-each> 
					<!-- <xsl:element name="{$name}"> -->
					<xsl:value-of select="ebforms:default" />
					<xsl:call-template name="write.instance">	
						<xsl:with-param name="path" select="." /> 
					</xsl:call-template>
				</xsl:element>			
			</xsl:otherwise>					
		</xsl:choose> 
	<!-- </xsl:if> --> 
	</xsl:for-each>
</xsl:template>

<!-- template to reconstruct the full path of an element -->  
<xsl:template name="getNodeset">
	<xsl:param name="nodeset" />
		<xsl:choose>
			<xsl:when test="parent::ebforms:element/@name!=''">
				<xsl:for-each select="parent::ebforms:element">
					<xsl:call-template name="getNodeset">
						<xsl:with-param name="nodeset" select="concat(ebforms:full-name,'/',$nodeset)"/>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$nodeset" /> 
			</xsl:otherwise>
		</xsl:choose> 
</xsl:template>

<!-- template to get the file name from a full file path -->
<xsl:template name="get.file.from.path">
	<xsl:param name="filepath" />
	<xsl:choose>
		<xsl:when test="contains($filepath,'/')">
			<xsl:call-template name="get.file.from.path">
				<xsl:with-param name="filepath" select="substring-after($filepath,'/')" />
			</xsl:call-template> 
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$filepath" />
		</xsl:otherwise> 
	</xsl:choose> 
</xsl:template> 

</xsl:stylesheet>